Is the load order for external javascript files different in IE8 versus IE7?

I am asking because I am running an application where I load an external script file in the HEAD section of the page and then try to call a function from it in the onLoad section of the BODY tag.

external.js

function someFunction()
{
   alert("Some message");
}

      

mypage.html

<html>
  <head>
    <script type="text/javascript" language="javascript" src="external.js"></script>
  </head>
  <body onLoad="someFunction();">
  </body>
</html>

      

Using developer tools in IE8 I am getting an exception thrown in the onLoad statement because apparently the external javascript file has not been loaded yet.

I haven't had this problem in IE7 yet, so my question is.

Did they change the load order between IE7 and IE8? If so, is there a better way to do it? (the real function refers to many other functions and constants that look much better in an external file)

Thanks, BJ

+2


source to share


3 answers


Well, I really feel pretty stupid.

It turns out the problem is not with the boot order. The problem was that the external javascript file had a syntax error in one of its functions, and apparently when the exception was thrown, it completely stripped the entire file, thereby leaving the rest of the functions inaccessible to the main page.

I'm not sure if this behavior is different from IE8 versus IE7, but it was a real problem anyway.



Thanks for your reply.

Bj

+2


source


I highly doubt that it was changed, it will break a significant number of sites.

Try this (without using developer tools): -



<body onload="alert(somefunction)">

      

this shouldn't break and will tell you if the id will be checked at the onload point somefunction

.

+1


source


Assuming what you think is happening is what is happening, you should try to attach body.onLoad later.

To keep it simple, you can do it from Prototype (including prototype, of course) with

Event.observe(window, 'load', function() { myFunction.init() });

      

or JQuery (including JQuery) with

$(document).ready(function(){
  // Your code here...
});

      

I think there is a clean Javascript way to do this, but the problem is that the body element doesn't exist yet, so it's rough ...

This goes to show that I had no problem loading the body onload in Javascript since IE8 and putting it in the body tag using external files. I'm going to check this out right now out of curiosity and I'll report back.

Edit: No problem executing onload from an external file. However, while we're at it, you can get familiar with JQuery, Prototype or Scriptaculous :)

0


source







All Articles