Javascript window.onload problem

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Javascript window.onload problem

Post by mchaggis »

Hi All,

First of all I'll state I'm a PHP coder that usually steers clear of javascript, however, I need some help with a problem.

I have a script that needs to execute a javascript function once the page has loaded and before everyone shouts out the following:

Code: Select all

<body onload="myfunction()">
or

Code: Select all

<script>
window.onload= new Function("myfunction()");
</script>
... there is a problem with the above methods. I have no control over the onload body tag as this script is designed to be included regardless of the what the designer has done for layout. Thus, in this case the designer is already using the onload body attribute to fix some style issues and besides I'd like it t me more independant. For eaxmple:

Code: Select all

<body onload="FixLayout()">
So my question is, can I manipulate the contents of window.onload and some how append additional events?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

add the function call to your existing onLoad function you could:

ex:

Code: Select all

<script>
function FixLayout()
{
   // do stuff for the fixlayout function
   // now call next function
   myFunction();
}
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Post by mchaggis »

That solution involves me editing the files that thedesigner has created... Yes I could instruct him to change his code however, the function I want to append to the onload is only to be called on one specific page, and the header is included on every page.

I have other solutions, like the ones I have already mentioned, however, I believe it should be possible to manipulate the contents of window.onload to do this
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

modifying a page anyway you must if code you want to add.

another alternative this is:

Code: Select all

<body onLoad="FixLayout(),myFunction()">
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

*SOLVED* Javascript window.onload problem

Post by mchaggis »

Well with a bit of trial and error, and I suppose a bit of luck I have solved my own problem...

The solution is below for anyone else that is interested

Code: Select all

<html>
<body onload="alert('Hello World')">

<script>
function MyNewFunction() {
        alert('This is my new function');
}

var oldonload = window.onload;
if (typeof window.onload != 'function') {
        window.onload = MyNewFunction;
} else {
        window.onload = function() {
                oldonload();
                MyNewFunction();
        }
}
</script>
</body>
</html>
Of course the code above doesn't do much but it does demonstrate what I wanted to do
Post Reply