Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Executing code before page load complete...

Reply
Thread Tools

Executing code before page load complete...

 
 
Mark Anderson
Guest
Posts: n/a
 
      10-23-2003
Problem in short: user is moving (clicking a link) from my page before some
JS code is run (to write a cookie).

The code does not run (in Body's onLoad event) until the page loads as there
are a number of images which can take a while to load on a slow connection.
Using fewer/different graphics is not an option!

The code writing a cookie the is called in the Body's onLoad event:

<body onLoad="setLast();">

The code setting up the cookie is in a function in the Head section

function setLast() {
var myPage = location.href;
//remove old value
deleteCookie('lastPage');
var expDate = new Date();
expDate.setTime( expDate.getTime() + ( myDuration * 60 * 60 * 1000 ) );
setCookie('lastPage', myPage,expDate,cookiePath);
}
....and it calls setCookie utility code in an external JS file also called in
the Head section:

<script src="../support/settings.js" language="JavaScript"
type="text/JavaScript"></script>

If I take the cookie setting code out of it's function wrapper but still in
the Head's <script> section will it execute as soon as it can? Or, would it
be wiser to put it inline in the Body's content in a <script> tag?
Presumably the Body onLoad checks for the external JS as well as the images,
whereas run-on-load might cause the code to execute before the external JS
was available and cause the code to fail?

I'd welcome knowledge from those who understand this rather than just reply
on some local testing and doubtless create a whole different problem

Regards

Mark



 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      10-23-2003
"Mark Anderson" <(E-Mail Removed)> writes:

> If I take the cookie setting code out of it's function wrapper but still in
> the Head's <script> section will it execute as soon as it can?


It should. As soon as it can is after all previous scripts have been
executed.

> Or, would it be wiser to put it inline in the Body's content in a
> <script> tag? Presumably the Body onLoad checks for the external JS
> as well as the images, whereas run-on-load might cause the code to
> execute before the external JS was available and cause the code to
> fail?


It shouldn't.

If you have more than one script tag, it won't execute the second before
the first have loaded. The second could depend on functions from the first,
and the first could contain document.write's that completely change
how the rest of the document is parsed.

I did hear of a problem with Netscape 6 where this didn't happen. The
scripts were loaded asynchroneously. The only solution for that
appeared to be to not rely on external scripts before the onload event
was triggered.

Netscape 6 was a very buggy browser.

/L
--
Lasse Reichstein Nielsen - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
 
Mark Anderson
Guest
Posts: n/a
 
      10-23-2003
Thanks. I'll try the code in the Head outside a function wrapper.

Luckily (?) for me the supported spec is for IE. I'm not deliberately
IE-centric but if there are Netscape, etc. issues in this particular
project the code will be passed on to somebody else's to play with in
slowtime. Not, I hasten to add that I'd want that.

Regards

Mark


"Lasse Reichstein Nielsen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Mark Anderson" <(E-Mail Removed)> writes:
>
> > If I take the cookie setting code out of it's function wrapper but still

in
> > the Head's <script> section will it execute as soon as it can?

>
> It should. As soon as it can is after all previous scripts have been
> executed.
>
> > Or, would it be wiser to put it inline in the Body's content in a
> > <script> tag? Presumably the Body onLoad checks for the external JS
> > as well as the images, whereas run-on-load might cause the code to
> > execute before the external JS was available and cause the code to
> > fail?

>
> It shouldn't.
>
> If you have more than one script tag, it won't execute the second before
> the first have loaded. The second could depend on functions from the

first,
> and the first could contain document.write's that completely change
> how the rest of the document is parsed.
>
> I did hear of a problem with Netscape 6 where this didn't happen. The
> scripts were loaded asynchroneously. The only solution for that
> appeared to be to not rely on external scripts before the onload event
> was triggered.
>
> Netscape 6 was a very buggy browser.
>
> /L
> --
> Lasse Reichstein Nielsen - (E-Mail Removed)
> DHTML Death Colors:

<URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
> 'Faith without judgement merely degrades the spirit divine.'



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing client script before page load Eric Livingston ASP .Net Building Controls 0 01-23-2008 05:06 PM
Executing a validating query before loading a page Patrick Olurotimi Ige ASP .Net 2 01-27-2005 06:00 PM
diaplay html page before executing js code max Javascript 0 01-05-2005 01:53 PM
Getting data from a usercontrol before containing page ends its page load et ASP .Net Building Controls 0 09-07-2004 04:12 AM
Does ASP.NET wait for the entire request body before executing the ASP page? Peter Strĝiman ASP .Net 2 10-28-2003 05:59 PM



Advertisments