473,396 Members | 2,018 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,396 software developers and data experts.

window.onload and extern js

In the head of my page I have:

<script type="text/javascript" src="scripts/test.js"></script>

and later in the head I have:

<script type="text/javascript">
window.onload = function() {
foo = 1;
};
</script>

If 'foo' is declared in test.js as
var foo = 0;

Will foo equal 1 when the page has finished loading? Or is the loading
of external javascript independent of when a page is counted as having
been loaded?

I'm going to be doing some DOM manipulation in the onload function so
I'm worried that if I instead trigger a function in the page from
test.js the function (in the page) might be ready but the page may not.
Andrew Poulos
Mar 14 '07 #1
7 1931
"Andrew Poulos" <ap*****@hotmail.comwrote in message
news:45**********************@per-qv1-newsreader-01.iinet.net.au...
In the head of my page I have:

<script type="text/javascript" src="scripts/test.js"></script>

and later in the head I have:

<script type="text/javascript">
window.onload = function() {
foo = 1;
};
</script>

If 'foo' is declared in test.js as
var foo = 0;

Will foo equal 1 when the page has finished loading? Or is the loading of external
javascript independent of when a page is counted as having been loaded?
I am unsure of the correct way in which to word this, hopefully someone else can
illuminate more clearly, but...

foo will still be 0 as the DOM is loaded, and then it appears external scripts are looked
at afterwards.

-Lost
Mar 14 '07 #2
-Lost <mi*********@comcast.netwrote:
foo will still be 0 as the DOM is loaded, and then it appears external
scripts are looked at afterwards.
i don't agree with u, javascript being executed from top to bottom of
the page see the following test :

--- index.html ---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>script exec</title>
<meta http-equiv="Content-type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript">
window.onload = function() {
foo = 1;
document.getElementById(' foo').innerHTML=" foo = " + foo;
};
</script>
</head>
<body bgcolor="#FFFFFF">
<h1>script exec</h1>
<h3 id="foo"></h3>
</body>
</html>
------------------

--- script.js ---
var foo=-1;
-----------------

Tested with Firefox 2.0.0.3 i got :

script exec
foo = 1

;-)

--
Une Bévue
Mar 14 '07 #3
ASM
Andrew Poulos a écrit :
In the head of my page I have:

<script type="text/javascript" src="scripts/test.js"></script>

and later in the head I have:

<script type="text/javascript">
window.onload = function() {
foo = 1;
};
</script>

If 'foo' is declared in test.js as
var foo = 0;

Will foo equal 1 when the page has finished loading?
Did you try it ?

I would say : yes
and the onload hasn't to be in a special script in the page
Or is the loading
of external javascript independent of when a page is counted as having
been loaded?
the external JS is loaded as soon as it is called
exactly as if it was scripted directly in the page at this place

when 2nd script tells foo = 1; on this moment foo changes from 0 to 1
I'm going to be doing some DOM manipulation in the onload function so
I'm worried that if I instead trigger a function in the page from
test.js the function (in the page) might be ready but the page may not.
best could be to add in your external JS what is necessary to start/fire
your manipulation function.

onload = function() {
foo++; // not more needed
domManipulator();
}
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Mar 14 '07 #4
Andrew Poulos wrote:
In the head of my page I have:

<script type="text/javascript" src="scripts/test.js"></script>

and later in the head I have:

<script type="text/javascript">
window.onload = function() {
foo = 1;
};
</script>

If 'foo' is declared in test.js as
var foo = 0;

Will foo equal 1 when the page has finished loading? Or is the loading
of external javascript independent of when a page is counted as having
been loaded?

I'm going to be doing some DOM manipulation in the onload function so
I'm worried that if I instead trigger a function in the page from
test.js the function (in the page) might be ready but the page may not.
Andrew Poulos
Only Firefox loads external javascript files asynchronously but from
what I've read the onload event only fires after these have finished
loading (and assumably executing).

I you have server side scripting then you could test by forcing the
delayed response on the javascript:

in the html file use:
<script type="text/javascript" src="scripts/test.js.php"></script>

with test.js.php:
<?php
// stop caching
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header('Content-type: text/javascript');
// wait 20 seconds
sleep(20);
// output true js file
readfile('test.js');
?>

Robin
Mar 14 '07 #5
""Une Bévue"" <un************@google.com.invalidwrote in message
news:1h**********************************@google.c om.invalid...
-Lost <mi*********@comcast.netwrote:
>foo will still be 0 as the DOM is loaded, and then it appears external
scripts are looked at afterwards.

i don't agree with u, javascript being executed from top to bottom of
the page see the following test :

--- index.html ---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>script exec</title>
<meta http-equiv="Content-type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript">
window.onload = function() {
foo = 1;
document.getElementById(' foo').innerHTML=" foo = " + foo;
};
</script>
</head>
<body bgcolor="#FFFFFF">
<h1>script exec</h1>
<h3 id="foo"></h3>
</body>
</html>
------------------

--- script.js ---
var foo=-1;
-----------------

Tested with Firefox 2.0.0.3 i got :

script exec
foo = 1
I tried the same thing:

<html>
<script src="loadjs.js" type="text/javascript"></script>
<script type="text/javascript">
// <![CDATA[
window.onload = function()
{
foo = 123;
}
// ]]>
</script>
<script type="text/javascript">
// <![CDATA[
alert(foo);
// ]]>
</script>
</html>

I still get "0." However, if I place the alert() after foo = 123; (and leave the other),
I get 0 then 123.

-Lost
Mar 14 '07 #6
ASM
-Lost a écrit :
>
I still get "0." However, if I place the alert() after foo = 123; (and leave the other),
I get 0 then 123.

-Lost

To be a twisted reasoning it is a twisted reasoning.

I understand why your name is 'Lost' !

I france we say : "With of the if, Paris could be put in bottle"
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Mar 14 '07 #7
On Mar 14, 7:59 am, "-Lost" <missed-s...@comcast.netwrote:
""Une Bévue"" <unbewusst.s...@google.com.invalidwrote in message

news:1h**********************************@google.c om.invalid...


-Lost <missed-s...@comcast.netwrote:
foo will still be 0 as the DOM is loaded, and then it appears external
scripts are looked at afterwards.
i don't agree with u, javascript being executed from top to bottom of
the page see the following test :
--- index.html ---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>script exec</title>
<meta http-equiv="Content-type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript">
window.onload = function() {
foo = 1;
document.getElementById(' foo').innerHTML=" foo = " + foo;
};
</script>
</head>
<body bgcolor="#FFFFFF">
<h1>script exec</h1>
<h3 id="foo"></h3>
</body>
</html>
------------------
--- script.js ---
var foo=-1;
-----------------
Tested with Firefox 2.0.0.3 i got :
script exec
foo = 1

I tried the same thing:

<html>
<script src="loadjs.js" type="text/javascript"></script>
<script type="text/javascript">
// <![CDATA[
window.onload = function()
{
foo = 123;}

// ]]>
</script>
<script type="text/javascript">
// <![CDATA[
alert(foo);
// ]]>
</script>
</html>

I still get "0." However, if I place the alert() after foo = 123; (andleave the other),
I get 0 then 123.

-Lost- Hide quoted text -

- Show quoted text -
The first alert(foo) is executed as it is found on the page which in
this case is before the onload has fired because the entire page has
not yet loaded... :) The second alert gets fired after the assignment
(obviously) which is also after onload has been called (obviously,
since it's in the onload event) which then displays the updated value
of 123 (obviously).

It's just a timing thing.

Mar 14 '07 #8

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

Similar topics

31
by: Benno Bös | last post by:
If I use the following construct in the frame "main" for a link to an extern site: <A HREF="http://www.any.xy" TARGET="extern"> the Browser is creating the window "extern", loading the page...
6
by: Brian | last post by:
Hi everyone, I'm writing a function (in javascript) that needs to do one thing if the page has not loaded, and another (different) thing if the page has already loaded. I'm looking for a way...
2
by: Jenny | last post by:
In the code below, I can write html content var t='<body BGCOLOR=blue>' for a new window. But if it contains javascript, such as var t='<body onload="window.open('new1.html')">', this code will...
3
by: Liming | last post by:
Hi, Here is my situation. I have a textarea on the page. When the page loads, I need it to have some default text (which will be generated dynamically) so I did something like this ...
3
by: Frances | last post by:
I have three functions I need triggered when page loads, so have <body onload="function1();function2();function3()"> but I want to take all these function calls out of body tag and call them...
6
by: Daz | last post by:
Hello everyone, I would like to open a child window from the parent, and add an onload event listener to the child window which will tell the parent when the document has loaded. As far as I...
20
by: Mark Anderson | last post by:
Hi, I have this in an external JS library: ///////////////////////// function addMyEvent(){ var obj; if(document.attachEvent) { obj = document.getElementsByTagName('img'); for...
5
by: lilOlMe | last post by:
Hi there! I'm developing some crazy Tab Control in .NET that uses JavaScript. A particular JavaScript method needs to be called during the window.onload event in order to initialize my Tab...
2
by: Beni Rose | last post by:
I'm trying to open a new window using window.open and then print that window once it's loaded. It works fine in Firefox, but not at all in IE. No matter what I put in my onload, it gets ignored....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.