Register  |  Login

Question Details    

   Question

Time: 19:35 - Oct 12, 2006     Asked by: jgivoni      Status: Answered      Points: 50   

Why doesn't OnLoad fire in Internet Explorer when script is loaded?

Consider this code:
<html>
<head>
 <title>test</title>
 <script src="test.js" onload="alert('ok')"></script>
</head>
<body>
</body>
</html>

We must presume that test.js exists of course.
According to MSDN the ONLOAD event should fire when the browser has loaded the object:
http://msdn.microsoft.com/workshop/autho...

(Notice the "Applies to: Script")

But it doesn't fire. At least not in my IE 6 browser.
(In FF 1.5 it does)

Anybody knows why? Or get different results?

Ask a New Question

Become a Quomon Expert

Current Categories

 

Other Questions Needing Answers


   

Answer Discussion
Answer Discussion
Answer Summaries
Answer Summary
 
I didn't know this should be possible, but couldn't you use the <body onload="javascript: alert('ok');"> instead?

Expert:

Anpanman

Date:

Oct 13, 2006

Time:

00:18

 

Votes: Good (0) | Bad (0)
Login to rate this answer

I could, except that my html files are generated by php and the body part comes from one file, the scripts included on a given page from another. Therefore I would prefer that the loading of a given script incur the execution of that script. Actually the event would be: onload="init()".
Now you could say that why don't I just include that line "init();" as the first one in the actual script?
Beacuse maybe I want to include the script without executing the init() in some cases.

Don't worry, there are plenty of other solutions, I just would like to know if anyone had knowledge about this specific issue.

I'll keep the question open...

Expert:

jgivoni

Date:

Oct 13, 2006

Time:

14:45

 

Votes: Good (0) | Bad (0)
Login to rate this answer

I would stay away from tags like that, and use something like this:
if (window.addEventListener) window.addEventListener('load',init,false);
else if (document.addEventListener) document.addEventListener('load',init,false);
else if (window.attachEvent) window.attachEvent('onload',init);
else {//Older browsers only
if (typeof window.onload=='function') {
var oldload=window.onload;
window.onload=function(){
oldload();
init();
}
} else window.onload=init;
}

Expert:

nhinds

Date:

Nov 01, 2006

Time:

00:35

 

Votes: Good (0) | Bad (0)
Login to rate this answer

nhinds, why would you stay away from tags like that?

And another thing; at what point and how do you suggest to run the script you propose?

Expert:

jgivoni

Date:

Nov 06, 2006

Time:

02:04

 

Votes: Good (0) | Bad (0)
Login to rate this answer

I avoid tags like onload because, in my opinion, they just pollute the markup (although that can be taken too far). Just throw that code in at the end of the javascript file you're including, or in its own script tag below the script with init in it

Expert:

nhinds

Date:

Nov 06, 2006

Time:

21:44

 

Votes: Good (0) | Bad (0)
Login to rate this answer

But nhinds,

if I put it in a script tag below the one that loads the script - wouldn't that just pollute the markup even more?

And if I put it inside the script itself, then I know that the script is already loaded, and I no longer need to attach any onload event to it!

Finally, my original question still stands uncommented:
Why doesn't the even fire - even though the documentation provided by the manufacturer states that it should?
I can see three posibilities so far:
1) I am reading the documentation wrong
2) I am reading the wrong documentation
3) It is a bug (may be limited to certain versions)

Expert:

jgivoni

Date:

Nov 21, 2006

Time:

12:32

 

Votes: Good (0) | Bad (0)
Login to rate this answer

It occurs in specific conditions which are unspecific. I create a simple HTML page and add an alert in body onload, it gives the alert but when I do the same in my ASP.NET application page (it has a lot of code) it does not work. May be some other javascript call falsifies it. Occurs only in IE 6.0. FF, Opera work well. If you find any solution, please post it to www.burqe.com contact us so it may reach me and I need it.

Expert:

haris

Date:

Aug 20, 2007

Time:

00:35

 

Votes: Good (0) | Bad (0)
Login to rate this answer

Question Answered

This question has been answered, and points have been rewarded to the following experts:

jgivoni: 50

You're welcome however to comment or give additional information or if you wish, you have the ability to write an Answer Summary for this question by clicking on the "Answer Summaries" Tab.

 
No summaries have been submitted yet. Want to be the first?



Respond to this question:

New User

  Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:


Forgotten Password

 

New User

  Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:


Forgotten Password

   

"Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it


All Questions


Language Options

English:

www.quomon.com

Español:

www.quomon.es

Quomon Blog

blog.quomon.com

Sponsors

Questions and Answers Software
Real Estate Postcards
Marketing Fulfillment