Forum Moderators: open

Message Too Old, No Replies

Merging multiple window.onloads?

         

JAB Creations

5:09 pm on Nov 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've been merging all my scripts in to a single master script file and I am finding that two scripts which use window.onload are conflicting (one won't let the other work when both are present). I have to keep the body element clear of attributes.

How do I execute both functions when the document loads?

Here are my two functions...

window.onload = externalLinks;
window.onload = findContent;

I tried...

window.onload = externalLinks; findContent;

...and that did not work.

Fotiman

5:46 pm on Nov 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



window.onload = function() {
externalLinks();
findContent();
});

JAB Creations

6:10 pm on Nov 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think that works (thanks even if it doesn't)...

Only problem now is I think one of my scripts broke!

I am having a problem with a focus script though now (or at least that part). It originally had it working and now it's not?

Anyway here is the code for that script alone...

HTML

<form action="" class="search" id="search" method="post">
<fieldset>
<input type="hidden" name="path" value="" />
<input type="hidden" name="refine" value="0" />
<input type="hidden" name="result_page" value="" />
<label class="search" for="query_string">Search Site</label>
<input id="query_string" maxlength="50" name="query_string" size="9" onfocus="javascript:if(this.value=='Search...') {this.value='';}" onblur="javascript:if(this.value=='') {this.value='Search...'}" tabindex="1" type="text" value="Search..." />
<input class="button" type="submit" value="Search" />
</fieldset>
</form>

CSS

<!--
//
// Search Focus
//

function addEvent(obj, evType, fn){
if (obj.addEventListener){
obj.addEventListener(evType, fn, true);
return true;
} else if (obj.attachEvent){
var r = obj.attachEvent("on"+evType, fn);
return r;
} else {
return false;
}
}

function findSearch()
{
if (! document.getElementById("search"))
{ return false;}
else
{ addEvent(window, 'load', function() { document.getElementById('query_string').focus()});}
}
window.onload = findSearch();

//-->

I'm not getting any errors...it should be finding the ID of Search which is on the form element.

Fotiman

6:22 pm on Nov 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




window.onload = findSearch();

Try this instead:

window.onload = findSearch;

(no parenthesis)

Note, however, that if you have previously defined window.onload, then this will overwrite it.

Fotiman

6:47 pm on Nov 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note, my first post had a typo:

window.onload = function() {
externalLinks();
findContent();
});

Should have been:

window.onload = function() {
externalLinks();
findContent();
};