Re: Functions assigned to variables test
- From: "tshad" <tfs@xxxxxxxxxxxxxx>
- Date: Sun, 16 Mar 2008 10:23:33 -0700
RobG wrote:
On Mar 16, 3:04 pm, "tshad" <t...@xxxxxxxxxxxxxx> wrote:
I have the following routine that I found but am not sure what it is
doing:
**********************
<html>
<head>
<title></title>
<script type="text/javascript">
function print()
{
alert("This is the startup routine from WindowOnLoad");
}
function printOnLoad()
{
alert("This is the Body onload routine");
}
function WindowOnload(f)
{
var prev = window.onload;
The initial value assigned to the window.onload property is the Null
object. If no other value has been assigned when WindowOnload is
called, prev will be assigned a reference to the Null object.
alert("prev = " + prev);
window.onload = function(){ if (prev)prev(); f(); }
}
WindowOnload(print);
This call is in a global context, likely it will run before the
opening body
tag is parsed so window.onload is null when WindowOnload is called.
Ok, that makes sense, if I understand you correcty.
All the Global variables and function calls will be called BEFORE the page
is parsed? Or in parsing the page since javascript was at the beginning of
the page is was run before the <body> tag had been seen? If I moved the
javascript to the bottom of the page would that solve the problem?
</script>
</head>
<body onload="printOnLoad();">
Here window.onload is defined, probably after WindowOnload has been
called.
</body>
</html>
*********************
It seems that this fuctions should check to see if there is an onload
function already defined. If so, it should run that routine first
then then the one that is passed (f).
"this functions"? I guess you mean WindowOnload.
Yes.
I want the WindowOnload function (which is on a master page of a template)
to be called before any other onload functions are called, then call the
already defined onload functions, then mine (the one that was passed).
Makes sense.
But what is happening is if run as is, it will do the alert, where
prev = null, and then will run the printOnLoad function. I am not
sure if this is coming from the <body ononload="printOnLoad();"> tag
or the WindowOnload function. I assume it is coming from the
WindowOnload function since the alert line that is printing prev is
done first (but I could be wrong here since prev is null so prev()
shouldn't be called - but f() should and isn't).
Probably because the opening body tag hasn't been parsed when
WindowOnload is called, so the window.onload property is null. When
it is parsed, window.onload is set and then when the event occurs some
time later, it is called.
But if I take the onload out of the <body> tag, it now runs the print
function which is the function passed but of course the printOnLoad
function won't run because it isn't called anywhere.
What is happening here?
WindowOnload is called before anything is assigned to window.onload.
Later, the opening body tag is parsed and window.onload is assigned an
anonymous function which calls printOnLoad. When the onload event
occurs, it calls the anonymous function which calls printOnLoad.
Move the script element inside the body element and you'll see a
difference.
Which script element?
Also, in this case, "print" doesn't get called at all - only PrintOnLoad
gets called. I assume this is because even though I add "print" to the
window.onload event, it gets overwritten when the parser gets to the body
tag and uses only the onload function that was originally set (in this case
the printOnLoad function).
But then, I would assume that if I take the onload event out of the <body>
tag altogether, nothing would get called. Because again it doesn't matter
what I set before the parser gets to the body tag, it would overwrite
whatever I set in my javascript routine with whatever was originally set (in
this case, nothing - <body>).
But that isn't what happens. If I take the onload out of the body tag, then
the "print" routine runs.
Doesn't seem very consistant - at least as I understand it.
Is there a way to make this work as I needed?
So as to add this function (print) into the onload function before (or
after) the current onload function. As you say, it won't know about the
onload function until after the global functions are called.
Thanks,
Tom
.
- Follow-Ups:
- Re: Functions assigned to variables test
- From: tshad
- Re: Functions assigned to variables test
- References:
- Functions assigned to variables test
- From: tshad
- Re: Functions assigned to variables test
- From: RobG
- Functions assigned to variables test
- Prev by Date: Re: Functions assigned to variables test
- Next by Date: Re: Is it possible to have "sourceless" multiple iframes on one page?
- Previous by thread: Re: Functions assigned to variables test
- Next by thread: Re: Functions assigned to variables test
- Index(es):
Relevant Pages
|