...

View Full Version : Onload



Tony M
08-30-2010, 05:47 AM
I have this code:


<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4c7b22470e521876"></script>

I want this JavaScript code above to be executed when all My page finished loading (maybe by using JavaScript onload)
So how can I do that? :confused:

Lerura
08-30-2010, 11:44 AM
You cant execute the js-file itself, but you can execute a function within it this way:

<body onload="FunctionName();">

DaveyErwin
08-30-2010, 01:40 PM
Put your code at the end of the page , just before </html>

Lerura
08-30-2010, 11:58 PM
Put your code at the end of the page , just before </html>

NO!

for two reasons:

Firstly the document is not fully loaded in until the </html> is reached, and therefore the code is executed during the load fase.

Secondly HTML do not support anything between the </body> and the </html> tags.
-------------------------
The structure of a HTML is roughly:

<!DOCTYPE>
<html>
<head>
title, metas, scripts and stylesheet goes here
</head>
<body>
text ,table, divs etc. goes here.
</body>
</html>
some scripts (excl functions) can be located in the BODY, if the are not affected by any other body content during the load-fase.

DaveyErwin
08-31-2010, 12:05 AM
yes, you are correct.
the script block should be
just before the body closing
tag just as it is at www.msn.com

Lerura
08-31-2010, 12:32 AM
That script is what I will call a tracker-script.
I has nothing do with the rest of the document.
It provide microsoft with some information of your browsing behavior, that they can use in statistics. Such as if you typed the address in or you followed a link.

DaveyErwin
08-31-2010, 12:43 AM
here is what Peter Michaux has to say....

The idea is that by the time the browser parses this final script element and runs the contained JavaScript, the elements that precede this final script element will be parsed and available as part of the DOM. This assumption based on de facto standard browser behavior and may not satisfy some developers following the letter of specifications. Many developers do depend on this behavior working in many scripts.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">

<head>
<title>Search Engines</title>
<script src="behavior.js" type="text/javascript"></script>
</head>

<body>
<h1>Search Engines</h1>
<ul>
<li id="google">Google</li>
<li id="yahoo">Yahoo!</li>
</ul>
<p><img src="hawaii.jpg" alt="hawaii"></p>
<script type="text/javascript">init();</script>
</body>

</html>behavior.js

function init() {
document.getElementById('google').onclick = function() {alert('Google');};
document.getElementById('yahoo').onclick = function() {alert('Yahoo!'););
}
It is even difficult to see the seven characters of JavaScript at the bottom of the HTML page. This is relatively good separation...


you will notice nothing is said of the dom not being ready

also can you provide a brief example demonstating any problem

de facto standard browser behavior means this is the way
browsers really work

Lerura
08-31-2010, 01:18 AM
if you look at the content of the function it specifies what must happen if a user takes an action and clicks on one of the related element.


the function could just as well have been triggered by <body onload="init();">
which is the proper way to do it.

In both cases, nothing will happen if the user clicks on the elements before the page has loaded,

But That's how microsoft are:
Always setting their own standards of what is proper coding.

DaveyErwin
08-31-2010, 01:48 AM
yes you are right, so
it really should be
written like this ...


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">

<head>
<title>Search Engines</title>
</head>

<body>
<h1>Search Engines</h1>
<ul>
<li id="google">Google</li>
<li id="yahoo">Yahoo!</li>
</ul>
<script type="text/javascript">
document.getElementById('google').onclick = function() {alert('Google');}
document.getElementById('yahoo').onclick = function() {alert('Yahoo!');}
</script>
<p><img src="hawaii.jpg" alt="hawaii"></p>
</body>
</html>

so no delay

Lerura
08-31-2010, 01:55 AM
Functions triggered by user-clicks can only be executed after the page has fully loaded, so this change will have no effect!

DaveyErwin
08-31-2010, 02:06 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">

<head>
<title>Search Engines</title>
</head>

<body>
<h1>Search Engines</h1>
<ul>
<li id="google">Google</li>
<li id="yahoo">Yahoo!</li>
</ul>
<script type="text/javascript">
document.getElementById('google').onclick = function() {alert('Google');}
document.getElementById('google').innerHTML = "hiyas";
document.getElementById('yahoo').onclick = function() {alert('Yahoo!');}
</script>
<p><img src="hawaii.jpg" alt="hawaii"></p>
</body>
</html>

you will never see google
(unless your javascript is off)
all you will see is hiyas
no user reaction

Lerura
08-31-2010, 02:14 AM
your added line will work since it requres no user action.

But why not use:
<li id="google">hiyas</li>
instead of changing it shortly after????



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum