Hello all
I'm experiencing some odd behavior in IE8 on a site I'm working on. In FF3.6, everything works fine, but when I load the page in IE8, I get a bunch of JS errors, and I've been unable to understand IE's error console.
It seems the 'Msn' object doesn't get loaded before it's used, which is odd, since the script that defines it is called before the one that uses it (it's all being retrieved in the "/contents/nav/cobrand/test_dk.js" script). That script does have a whole mess of "[if IE x.y]" conditional comments, so that could be it, but I didn't write it, so I'm a little lost in there.
- Did you try moving all of the script to the bottom of the page?
- I can't do that, unfortunately :(
2 answers
points
I finally solved the problem, and I guess I owe it to post the solution(s) here - since it turned out there were several issues:
- I learned that IE doesn't execute javascripts in the same order as they are referenced, if they are referenced by
document.write()'ing the script tags. In the end, I was forced to concatenate all the scripts in the .js file instead of usingdocument.write() - I was using jQuery functions inside external html pages that I was loading via Ajax (
.load()), and as far as I could tell, IE sandboxes the loaded content so it doesn't have the same context as the original page (where jQuery was loaded). So jQuery's$()function didn't work in the externally loaded content. The solution: Moving all the jQuery to the original page, and using the callback parameter ofload(url, callback)to call the jQuery code after the external content was loaded. Not very elegant, but it works.
point
Just because the script that defines a function is included before the script that uses the function doesn't mean that the function won't be called before it's completely prepared. There is a reason that jQuery and other libraries that do async calls include a callback function. You can guarantee that your library is fully loaded and ready before attempting to use it then. The jQuery code (http://api.jquery.com/jQuery.getScript/) Looks like this:
function callback() {
//use the library.
}
$.getScript("URL", callback);
- Actually, I solved the problem myself earlier today (finally), but your solution would have solved it, so I'm upvoting it. Thanks!
- We went through this a *LOT* in the last project I was on. We wrote an AJAX system management interface for routers. Hence I was able to answer it immediately. :-)
