Results 1 to 6 of 6

Thread: Dynamically Loading External JavaScript and CSS Files

  1. #1
    Join Date
    Nov 2009
    Posts
    712

    Dynamically Loading External JavaScript and CSS Files

    Hello, I want to know the information by using which I can able to Dynamically Load External JavaScript and CSS Files. I have tried to do this, but I am not able to get the solution for this problem. If you are having any solution from which I can able to achieve it, then I will be thankful to you.

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Dynamically Loading External JavaScript and CSS Files

    If you want to load the Javascript or the css file dynamically then it is necessary to to create a swanky new script or a link element in your nutshell and then by making use of the appropriate attributes or parameters you will able to get the solution for your problem. Then it is also necessary to make use of the element.appendChild() method in your program and you will be able to add the element to the location which you want. So, just follow it and you will able to add dynamically external javascript and css files.

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: Dynamically Loading External JavaScript and CSS Files

    I have the following programs which are simple to understand the dynamic loading of the external javascript and css file. So, just make use of it and get your problem solved:
    Javascript code:
    Code:
    var start="Game"
    alert("First Name: " + start)
    Css file code:
    Code:
    #demotable first
    {
    background-color: black;
    }
    
    #demotable second
    {
    color: blue;
    }

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Dynamically Loading External JavaScript and CSS Files

    Hey, use the code below if you want to Dynamic Javascript Insertion :
    Code:
    var Id = document.getElementsByTagName("head")[0];         
    var NScript = document.createElement('script');
    NScript.type = 'text/javascript';
    NScript.src = 'http://www.techarena.in/testscript.js';
    Id.appendChild(NScript);

  5. #5
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Dynamically Loading External JavaScript and CSS Files

    Hello, If you make use of the code below then you will able to get to load the external files into DIV:
    Code:
    function Test(URL, destination)
     {
      document.getElementById(destination).innerHTML = 'Sorting of data';
      if (window.XMLHttpRequest)
     {
    request = new XMLHttpRequest();
      } 
    else if (window.ActiveXObject)
     {
    request = new ActiveXObject("Microsoft.XMLHTTP");
      }
      if (request != undefined)
     {
    request.onreadystatechange = function()
     {
    TestDone(URL, destination);
    };
    request.open("GET", URL, true);
    request.send("");
      }
    }  
    function TestDone(URL, destination) 
    {
      if (request.readyState == 4)
     { 
    if (request.status == 200) 
    { 
      document.getElementById(destination).innerHTML = request.responseText;
    } 
    else 
    {
    document.getElementById(destination).innerHTML=" Testing Error:\n"+ request.status + "\n" +request.statusText;
    }
      }
    }
    function load(name, div) 
    {
    Test(name,div);
    return false;
    }

  6. #6
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Dynamically Loading External JavaScript and CSS Files

    If you want to load the javascript file dynamically then you need to simply make use of the small code as given below:
    Code:
    <script type="text/javascript">
    function addScript(url)
    {
       document.write('<script src="', url, '" type="text/JavaScript"><\/script>');
    }
    addScript("test.js");
    </script>

Similar Threads

  1. How to calculate value dynamically in JavaScript?
    By KAIRU26 in forum Software Development
    Replies: 5
    Last Post: 24-02-2010, 11:01 PM
  2. Dynamically Loading a C# DLL
    By Allan.d in forum Software Development
    Replies: 3
    Last Post: 23-11-2009, 07:54 AM
  3. How to dynamically expand the javascript height?
    By Recko in forum Software Development
    Replies: 3
    Last Post: 09-07-2009, 08:19 PM
  4. Javascript to rewrite the function dynamically
    By djbbenn in forum Software Development
    Replies: 2
    Last Post: 18-06-2009, 10:19 AM
  5. How to add forms Dynamically with JavaScript
    By Neil'o in forum Software Development
    Replies: 3
    Last Post: 12-03-2009, 01:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,205,806.07788 seconds with 17 queries