Monday, June 3, 2024
124
rated 0 times [  131] [ 7]  / answers: 1 / hits: 62102  / 14 Years ago, sun, january 30, 2011, 12:00:00

I've been using this function to attach onload handler to a script tag, it seems to be the recommended way over the internet.

Yet, it doesn't work in internet explorer, if page is loaded already (tested in ie 8). You can see that it works in normal browsers (fires alert when script is loaded).



Am I missing something?

Thank you


More From » internet-explorer

 Answers
35

You should call jQuery.getScript, which does exactly what you're looking for.



EDIT: Here is the relevant source code from jQuery:



var head = document.getElementsByTagName(head)[0] || document.documentElement;
var script = document.createElement(script);
if ( s.scriptCharset ) {
script.charset = s.scriptCharset;
}
script.src = s.url;

// Handle Script loading
var done = false;

// Attach handlers for all browsers
script.onload = script.onreadystatechange = function() {
if ( !done && (!this.readyState ||
this.readyState === loaded || this.readyState === complete) ) {
done = true;
jQuery.handleSuccess( s, xhr, status, data );
jQuery.handleComplete( s, xhr, status, data );

// Handle memory leak in IE
script.onload = script.onreadystatechange = null;
if ( head && script.parentNode ) {
head.removeChild( script );
}
}
};

// Use insertBefore instead of appendChild to circumvent an IE6 bug.
// This arises when a base node is used (#2709 and #4378).
head.insertBefore( script, head.firstChild );

[#93981] Friday, January 28, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
myrap

Total Points: 407
Total Questions: 105
Total Answers: 109

Location: Cambodia
Member since Thu, Oct 7, 2021
3 Years ago
myrap questions
Tue, Feb 8, 22, 00:00, 2 Years ago
Wed, Jan 15, 20, 00:00, 4 Years ago
Thu, Oct 24, 19, 00:00, 5 Years ago
Thu, Oct 3, 19, 00:00, 5 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;