www.webdeveloper.com
Results 1 to 6 of 6

Thread: load .js from javascript

  1. #1
    Join Date
    Jul 2004
    Posts
    44

    load .js from javascript

    Hello, I need to load a .js file in a determinated moment with javascript.

    The idea is :

    <script>
    Code:
    function t()
    {
        // load js file
    }
    
    </script>
    <button onclick="t()">test</test>

    When I press the button, I want to load a js file.
    I had created a script element dynamically like this:

    Code:
    var sc2 =window.document.createElement("script");
    sc2.setAttribute("src","/jsherpa/webware/scripts/datefmt.js"); 
    sc2.setAttribute("type","text/javascript");
    window.document.getElementsByTagName("head")[0].appendChild(sc2); 
    window.calendar();
    but when I call the calendar function, the .js is not loaded still.

    Do you know any solution?

    Thanks

  2. #2
    Join Date
    Nov 2002
    Posts
    4,473
    If you have placed the creation code inline, I do not think it will work because the <HEAD> does not exist until the </HEAD> has been read. If you use the onload event to create your script element, it should work. Here is my test page:
    Code:
    <head>
    <script>
    function init() {
      var sc2 = document.createElement("script");
      sc2.setAttribute("type", "text/javascript");
      sc2.setAttribute("src", "test.js");
      document.getElementsByTagName("head")[0].appendChild(sc2);
    }
    </script>
    </head>
    <body onload="init()">
    This is a test to add a script element to the DOM
    </body>
    and the file test.js:
    Code:
    alert("Hello World!");

  3. #3
    Join Date
    Jul 2004
    Posts
    44
    Hello,

    thanks for you reply, but the problem is other:

    If I have this .js:

    Code:
    function test()
    {
       alert("test")
    }
    And in the html I have:

    Code:
    var sc2 = document.createElement("script");
      sc2.setAttribute("type", "text/javascript");
      sc2.setAttribute("src", "test.js");
      document.getElementsByTagName("head")[0].appendChild(sc2);
    test()
    when the test function is called, the .js is not loaded still.

    I would need to call the test function when the js is loaded...

  4. #4
    Join Date
    Nov 2002
    Posts
    4,473
    You really have not changed anything from your original problem except what the script is supposed to do. My example is a complete solution, and I tested it on Windows XP Professional with IE 6.

    You need to create the script after the page has otherwise finished loading.

    Your script should be giving you an error. In IE, you should be seeing a yellow triangle in the lower left corner of the window. If you click on the yellow triangle, you will see an error like "object expected". That is because the <HEAD> tag does not exist yet.

    I have attached a zip file with my example.
    Attached Files Attached Files

  5. #5
    Join Date
    Jul 2004
    Posts
    44
    Hello,

    sorry, but I didn't tell you very well...

    If I write your code, the example runs correctly.
    But if I have this code:

    test.js
    Code:
    function test()
    {
    	alert("tt")
    }
    And the html:

    Code:
    <head>
    <script>
    function init() {
      var sc2 = document.createElement("script");
      sc2.setAttribute("type", "text/javascript");
      sc2.setAttribute("src", "test.js");
      document.getElementsByTagName("head")[0].appendChild(sc2);
      test();
    }
    </script>
    </head>
    <body onload="init()">
    This is a test to add a script element to the DOM
    </body>
    The function test is called after creating the script element, but in this moment, the test.js file is not loaded yet, because it need some time to load.
    It occurs only in web, in local runs web, because the file is loaded very fast.

    Does I tell me well?

    Thanks for your patience and sorry for my english .

  6. #6
    Join Date
    Nov 2002
    Posts
    4,473
    Ok, now I understand your dilemma.

    I could not find any reference for an "onload" event in a script tag. I guess you will have to find another way to do what you are trying to do.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles