473,387 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Executing code before page load complete...

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

Jul 20 '05 #1
2 29791
"Mark Anderson" <ma**@notmeyeardley.demon.co.uk> 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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
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" <lr*@hotpop.com> wrote in message
news:wu**********@hotpop.com...
"Mark Anderson" <ma**@notmeyeardley.demon.co.uk> 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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: BK | last post by:
Can someone point me to a code sample that illustrates executing long running tasks (asynchronous) from a web application in ASP.NET? I assume that Web Services might come into play at some point,...
4
by: chris.dunigan | last post by:
I'm looking for an example of how to execute an existing DTS­ package from an ASP (VB)script and would appreciate any and all response. ­I don't even know if it's possible Thanks - Chuck...
2
by: mart | last post by:
I am experiencing a weird problem with some buttons on my webpage. The page_load is executing twice when a button is clicked. If I replace the button for a Linkbutton the page_load only loads...
7
by: Jibey | last post by:
Hello: I'm facing a very strange problem. When I run my Web application in Visual Studio.NET the Page_Load event is not executing. Other events like a Button_Click are executing. It doesn't...
11
by: simon | last post by:
Hello, I'm helping write a .net app, we are using VS2005 and asp/vb.net for code we are using the "masterPage.master" technique, within the masterpage we have an asp:contentplaceholder that is...
7
by: tshad | last post by:
I thought I understood how the SaveViewState is working and was trying to use this (as per some code I found) to detect refreshes. It seemed to be working but I found that the SaveViewState was...
8
by: dd | last post by:
Has anyone found a way around the problem IE has if you create elements (script or div, doesn't seem to matter) BEFORE the document.readyState is "complete" ? I know you can sometimes get away...
14
by: lmttag | last post by:
Hello. We're developing an ASP.NET 2.0 (C#) application and we're trying to AJAX-enable it. We're having problem with a page not showing the page while a long-running process is executing. So,...
13
by: mowsen | last post by:
Hello Group, i'm using a little "ajax" loader script to dynamically load files into different "div" tags on my main site. the code for this part looks like: function loader() { var args =...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.