Lady_M
asked on
window.onload in IE6 - does it work?
Hi
I've asked this question in a roundabout way in a couple of other threads but I'm not getting to the bottom of it. It seems pretty straightforward but the info on the web gets pretty complex.
So I thought I'd keep it simple: can you use 'window.onload' in IE6?
I want to use it in the head of a page to initialise 3 functions that are in an external js file.
I can't get it working. So, if not, what do you do instead?
Thanks
I've asked this question in a roundabout way in a couple of other threads but I'm not getting to the bottom of it. It seems pretty straightforward but the info on the web gets pretty complex.
So I thought I'd keep it simple: can you use 'window.onload' in IE6?
I want to use it in the head of a page to initialise 3 functions that are in an external js file.
I can't get it working. So, if not, what do you do instead?
Thanks
ASKER
Hi thanks for getting back to me. I've read one of those articles before, but it does my head in. I don't get what the point of that init() is and where you put your function initiations. Do you?
you write init() and include it as a js include. the contents of the function are basically what you wanted to do inside the window.onload event. the idea here is that you just call a custom function, rather than using window.onload.
ASKER
Hi again.
But it says that the init file should just contain one line:
init();
Are you saying that isn't the case and actually that init function should list my three function calls? That makes sense.
If so, what would the exact syntax in that init() function be please?
init (){
initiate function 1(arg, arg);
initiate function 2 ();
initiate function 3();
}
But it says that the init file should just contain one line:
init();
Are you saying that isn't the case and actually that init function should list my three function calls? That makes sense.
If so, what would the exact syntax in that init() function be please?
init (){
initiate function 1(arg, arg);
initiate function 2 ();
initiate function 3();
}
i think you're thinking about this the wrong way. let's step back.
you wanted to do something using window.onload. so, you were likely trying something like this:
window.onload(callMyFuncti on());
then, the body of callMyFunction is whatever JavaScript you wanted to do when the window loaded.
since window.onload is not valid in IE6, you have to put a function call in the HTML somewhere, so that when the page loads, it will do something. so, you do that <script> include that includes the javascript code containing the init() function. then, in the init function, you do whatever you were going to do inside the callMyFunction.
does that make sense?
the example i sent you doesn't know what you were trying to do once you did window.onload. it was just trying to show an alternative method of calling some function once the page loads. after that, it's up to each person to decide what to do in the function.
you wanted to do something using window.onload. so, you were likely trying something like this:
window.onload(callMyFuncti
then, the body of callMyFunction is whatever JavaScript you wanted to do when the window loaded.
since window.onload is not valid in IE6, you have to put a function call in the HTML somewhere, so that when the page loads, it will do something. so, you do that <script> include that includes the javascript code containing the init() function. then, in the init function, you do whatever you were going to do inside the callMyFunction.
does that make sense?
the example i sent you doesn't know what you were trying to do once you did window.onload. it was just trying to show an alternative method of calling some function once the page loads. after that, it's up to each person to decide what to do in the function.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Hi jmanGJHS97
Thanks, but I'm afraid I really don't understand. V. confused.
Could you do me a favour and spell it out to me if you have time please? ie: "put this in the html head, put this in the external js and put this in the init.js page".
You can't assume I know the syntax either.
Thankyou
So my html head looks like this:
<script type="text/javascript" language="javascript" src="/global/scripts/globa l.js"></sc ript>
<script>
window.onload = function() { rolloverButtons(); rolloverMenuL2 (); populateBox (1.html', null); };
</script>
And my /global.js looks like this:
// JavaScript Document
<script language="JavaScript" type="text/javascript" defer="defer">
//1. Rollover Menu: Show list on hover.
rolloverButtons = function() {
alert ("rollover buttons");
var sfEls = document.getElementById('n avbuttons' ).getEleme ntsByTagNa me('LI');
for (var i=0; i<sfEls.length; i++) {
sfEls[i].onmouseover=funct ion() {
this.className+=" sfhover";
}
sfEls[i].onmouseout=functi on() {
this.className=this.classN ame.replac e(new RegExp(" sfhover\\b"), "");
}
}
}
//2. Rollover Top Nav Menu: Show second level of drop-down menu to IE by assigning class to #header #banner ul#navL2 li
rolloverMenuL2 = function() {
alert ("rollover menu");
var sfEls = document.getElementById('n avL2').get ElementsBy TagName('L I');
for (var i=0; i<sfEls.length; i++) {
sfEls[i].onmouseover=funct ion() {
this.className+=" over";
}
sfEls[i].onmouseout=functi on() {
this.className=this.classN ame.replac e(new RegExp(" over\\b"), "");
}
}
}
//3 Post file into box, uses prototype.js which should be in same directory.
function populatebox (serverPage, obj){
alert ("populate box");
var objID = "boxtext";
new Ajax.Updater(objID, serverPage);
//change tab color
if(obj != null)
{
var links = document.getElementById('b ox').getEl ementsByTa gName('A') ;
for (var i=0; i<links.length; i++) {
if (links[i].className=='high lighted' && links[i] != obj)
links[i].className='plain' ;
}
obj.className='highlighted ';
}
}
</script>
Thanks, but I'm afraid I really don't understand. V. confused.
Could you do me a favour and spell it out to me if you have time please? ie: "put this in the html head, put this in the external js and put this in the init.js page".
You can't assume I know the syntax either.
Thankyou
So my html head looks like this:
<script type="text/javascript" language="javascript" src="/global/scripts/globa
<script>
window.onload = function() { rolloverButtons(); rolloverMenuL2 (); populateBox (1.html', null); };
</script>
And my /global.js looks like this:
// JavaScript Document
<script language="JavaScript" type="text/javascript" defer="defer">
//1. Rollover Menu: Show list on hover.
rolloverButtons = function() {
alert ("rollover buttons");
var sfEls = document.getElementById('n
for (var i=0; i<sfEls.length; i++) {
sfEls[i].onmouseover=funct
this.className+=" sfhover";
}
sfEls[i].onmouseout=functi
this.className=this.classN
}
}
}
//2. Rollover Top Nav Menu: Show second level of drop-down menu to IE by assigning class to #header #banner ul#navL2 li
rolloverMenuL2 = function() {
alert ("rollover menu");
var sfEls = document.getElementById('n
for (var i=0; i<sfEls.length; i++) {
sfEls[i].onmouseover=funct
this.className+=" over";
}
sfEls[i].onmouseout=functi
this.className=this.classN
}
}
}
//3 Post file into box, uses prototype.js which should be in same directory.
function populatebox (serverPage, obj){
alert ("populate box");
var objID = "boxtext";
new Ajax.Updater(objID, serverPage);
//change tab color
if(obj != null)
{
var links = document.getElementById('b
for (var i=0; i<links.length; i++) {
if (links[i].className=='high
links[i].className='plain'
}
obj.className='highlighted
}
}
</script>
i would say the easiest way to do this is to just take the 3 function calls you have in your window.onload function and put them into the body onload event, like this:
<body onload="rolloverButtons(); rolloverMenuL2 (); populateBox (1.html', null);">
then, just delete this:
<script>
window.onload = function() { rolloverButtons(); rolloverMenuL2 (); populateBox (1.html', null); };
</script>
that should do the same thing for you. all the example was doing is putting those 3 function calls into another function, but you could just call them in the body tag, like i show here.
<body onload="rolloverButtons();
then, just delete this:
<script>
window.onload = function() { rolloverButtons(); rolloverMenuL2 (); populateBox (1.html', null); };
</script>
that should do the same thing for you. all the example was doing is putting those 3 function calls into another function, but you could just call them in the body tag, like i show here.
i'm not sure what this is:
1.html'
there is a trailing single quote, which will cause an error. also, you can't name an element with a number.
if you have a text box like this:
<input type="text" id="myTextBox" value="" />
then you can call your populateBox like this:
populateBox(document.getEl ementById( 'myTextBox ).innerHTM L, null);
but, this question is about the window.onload event, not debugging your javascript. that stuff is better suited for another question in EE.
1.html'
there is a trailing single quote, which will cause an error. also, you can't name an element with a number.
if you have a text box like this:
<input type="text" id="myTextBox" value="" />
then you can call your populateBox like this:
populateBox(document.getEl
but, this question is about the window.onload event, not debugging your javascript. that stuff is better suited for another question in EE.
ASKER
Yes, I really just want to get the initialisation of the external functions working, that's all. I will worry about code problems later. (incidentally, the file isn't really called 1.html, that's just for convenience here).
But if you can give me some more help on initialising the 3 functions please, if you have time, that would be much appreciated as that is the major issue at the moment.
Thanks
But if you can give me some more help on initialising the 3 functions please, if you have time, that would be much appreciated as that is the major issue at the moment.
Thanks
what exactly is not working? i provided an example that will call the 3 functions when the page loads. that seemed to me to be the crux of your question.
ASKER
Hi jmanGJHS97, yes you did, sorry I missed a couple of your replies. I see what you're doing now thanks.
Not a problem. Glad it's working for you.
http://blog.outofhanwell.com/2006/06/08/the-windowonload-problem-revisited/
http://www.codingforums.com/archive/index.php?t-76716.html