SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamically Add JavaScript onload problem with IE7

    Hello

    I am trying to write a script to dynamically add a JavaScript file. This script works as is in FireFox but it does not work in IE7 (or I should say, it does not call scriptLoaded and there is not a JavaScript error).

    If I remove the newScript.onload everything works but I would really like it to fire the sciprLoaded function once the script loads. Does someone know why this does not work in IE7 but it does work in FireFox?

    Thanks in advance for your time.

    Code:
    <html>
    <head>
    
    <script language="JavaScript">
    function loadJavaScript()
    {
       var headID = document.getElementsByTagName( "head" )[0];
       var newScript = document.createElement( "script" );
       
       newScript.type = "text/javascript";
       newScript.onload = function(){ scriptLoaded() };
       newScript.src = "hello.js"
       
       headID.appendChild( newScript );
    }
    
    function scriptLoaded()
    {
        alert("loaded");
        hello();
    }
    </script>
    
    </head>
    <body>
    
    <a href="javascript: loadJavaScript();">Dynamically Load JavaScript</a>
    
    </body>
    </html>
    Hello.js
    Code:
    function hello()
    {
       alert("Hello!!!");
    }

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To track the loaded progress of a script in IE you need to examine the readyState of the script element from its readystatechange event. There is no onload for scripts or linked files in IE. Better check MSDN for the latest- it is a moving target. For now, ajax methods are the most reliable cross browser way to track importing files to a document.

  3. #3
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the feedback. I will have to do more research on that.


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
  •