Jump to content

onload problem


djp1988

Recommended Posts

Hi, I have a script which is set to start on load of the window, but the script isn't working, and so I put an alert(); as the first line of the function, and I can see that the window.onload = init; isn't working, but why?http://code.herpfrance.com/france%20copy/france.html

Link to comment
Share on other sites

Bonjour, it's just a simple syntax error, look basically the top of your script says this.

window.onload = init;function init() 

See what's wrong yet? It should be really be in your face right now. :) Highlight the black area if you can't figure it out from there.

window.onload = init; with the extra brackets...window.onload = init(); Voíla!

Link to comment
Share on other sites

Oh, that's strange, I was working on some other scripts before and had the onload line like the way it is and it was working, but not for this script.... ok thanks

Link to comment
Share on other sites

If you click on the lowest region on the map, in the middle, the Midi-Pyrénées div apears, I want to get that book cover to perform the image zoom script but it appears to not work, and when i alert the images.length, onload it tells me 0, there should be 2, the map and the book, why is it telling me 0 ?Also, on another script I have the window.onload = init; works like that and doesn't work if I add () and on this script it's the other way round, why?

Link to comment
Share on other sites

window.onload = init();
Wrong. DJP, you were correct the first time. Kingy's method actually run's init() immediately, when it reads this line of the script, and attempts to assign the return value (which is false) to window.onload. And naturally, since the window has not loaded yet, when the function runs there are no images to count. If you want to do it Kingy's way, the expression needs to go in quotes. Otherwise, leave off the parentheses.Now, when I write it the correct way, the script works for me in Firefox 3.01Consider changing this line:
if(images[i].className == "bookCover"){

to this:

if( (images[i].className == "bookCover") || (images[i].class == "bookCover") ){

Some browsers use the non-compliant class property instead of className. I meant to mention that yesterday, but that thread took a different turn and I forgot.

Link to comment
Share on other sites

I found my problem, I had an inline onload for the body tag, could you explain this part: If you want to do it Kingy's way, the expression needs to go in quotes. how would that look?

Link to comment
Share on other sites

I don't think it's the problem, but just to be sure, I think that you should put the onload event after init() is declared to make sure it exists:function init() {[...]}window.onload = init;And maybe "images" is already reserved, try calling the variable something else:imgs = document.getElementsByTagName("img");

Link to comment
Share on other sites

Yup, I never noticed that body tag before, but that declaration will certainly clobber the first one. But it also shows that you already know how to put an onload declaration in quotes, because it's just the way you do it in the body tag. Assigning an event handler in a tag actually puts javascript in the tag, so the format is not really different.Anyway, here are the correct ways to do it in your script. window.onload = "init();" window.onload = init;

Link to comment
Share on other sites

Okay, well because I want to do 2 functions on load I have this now:

window.onload = StartThisPage;function StartThisPage(){	init();	startupimg();	}

BUT in an attempt to seperate mark up form behavior, I have this in my external .js file and it's not making the onload work now, what is the problem?

Link to comment
Share on other sites

You can try using addEventListener(), and Internet Explorer's alternative attachEvent(). You can call more than one function with the same event like that.

function testing(e) {alert(1);}function test(e) {alert(2);}if(window.addEventListener) {window.addEventListener("load",testing,true);window.addEventListener("load",test,true);} else if(window.attachEvent) {window.attachEvent("onload",testing);window.attachEvent("onload",test);}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...