...

View Full Version : Can <body onload> be modified?



listerdl
03-24-2012, 02:27 PM
What I mean is that my <body> tag currently is like this:


<body class="page-course">

I would like to insert a pop-up contact form which requires the body tag to be like this:


<body onload="javascript:fg_hideform('fg_formContainer','fg_backgroundpopup');">

Not quite sure how to merge these - indeed can I merge them?

Perhaps better to make the body class (top example) execute elsewhere?

THANKS!!

DaveyErwin
03-24-2012, 02:38 PM
<html>
<head>
<style>
.page-course{background:blue;}
</style>
<script type= "text/javascript">
function fg_hideform(a,b){alert(b)}

</script>
</head>

<body onload="fg_hideform('fg_formContainer','fg_backgroundpopup');" class="page-course">
</body>
</html>

listerdl
03-24-2012, 02:44 PM
Thanks I appreciate your help.

What is this for please:

<script type= "text/javascript">
function fg_hideform(a,b){alert(b)}

Seems that you have done something really clever there?

(Im still beginner level)

THANKS





<html>
<head>
<style>
.page-course{background:blue;}
</style>
<script type= "text/javascript">
function fg_hideform(a,b){alert(b)}

</script>
</head>

<body onload="fg_hideform('fg_formContainer','fg_backgroundpopup');" class="page-course">
</body>
</html>

felgall
03-24-2012, 11:29 PM
What is this for please:

<script type= "text/javascript">
function fg_hideform(a,b){alert(b)}

Seems that you have done something really clever there?

(Im still beginner level)



That's a very simple JavaScript function defined in the one of the three ways that JavaScript allows functions to be defined that is the least flexible but most commonly used by beginners (Just as only beginners would put the onload in the body tag rather than in the JavaScript itself).

The alert(b) is just a placeholder for your popup form to allow you to test that the code to that point works.

Mishu
03-24-2012, 11:55 PM
(Just as only beginners would put the onload in the body tag rather than in the JavaScript itself).


What's the difference where you put it? I also would put it in the javascipt but only to keep the javascript separate from the html. I see very many experienced and competent coders put it in the body tag though.

felgall
03-25-2012, 07:06 PM
What's the difference where you put it? I also would put it in the javascipt but only to keep the javascript separate from the html. I see very many experienced and competent coders put it in the body tag though.

The difference is efficiency and the amount of JavaScript required.

Modern browsers can load up to eight files at a time as long as none of them are JavaScript. When A JavaScript file loads all the rest of the page has to wait until that file finishes loading by itself. By placing the JavaScript at the bottom the rest of the page loads much faster.

Also most JavaScript can't run until at least all the HTML has loaded and so if placed higher in the page needs to be attached to a handler that tests if the page has loaded or not before running it. At the bottom it can run straight away in many cases and so runs sooner than if it were attached higher in the page. This does away with the need for onload completely in 99%+ of cases.

No programmer who is experienced and competent with JavaScript would embed JavaScript in the actual HTML or place the script tags higher in the page than they need to be. Doing either of these means that they might have learnt 20th Century JavaScript and be competent at writing scripts suitable for Netscape 4 but have failed to keep up with all the changes in the language.

Mishu
03-26-2012, 07:27 AM
By placing the JavaScript at the bottom the rest of the page loads much faster.


With the amount of javascript I usually write the time difference is very, very small.

felgall
03-26-2012, 08:33 AM
With the amount of javascript I usually write the time difference is very, very small.

It isn't the size of the JavaScript so much as the size of all the other files that could be downloading but are waiting for a file to finish downloading so the JavaScript can start downloading.

If you have one 10Mb image that just starts downloading followed by a 10 byte JavaScript and then seven more 10Mb images then those seven have to wait for the JavaScript to finish. Move the JavaScript to the end and all eight images download almost simultaneously followed by the JavaScript - effectively halving the download time.

Philip M
03-26-2012, 11:03 AM
It isn't the size of the JavaScript so much as the size of all the other files that could be downloading but are waiting for a file to finish downloading so the JavaScript can start downloading.

If you have one 10Mb image that just starts downloading followed by a 10 byte JavaScript and then seven more 10Mb images then those seven have to wait for the JavaScript to finish. Move the JavaScript to the end and all eight images download almost simultaneously followed by the JavaScript - effectively halving the download time.

Absolutely right, but your example is a bit contrived. :)

Mishu
03-26-2012, 11:10 AM
It isn't the size of the JavaScript so much as the size of all the other files that could be downloading but are waiting for a file to finish downloading so the JavaScript can start downloading.


Yes I understand what you are saying but normally the amount of javascript I use is so small that I don't notice any difference in the loading time for a page no matter where I put the javascript.

felgall
03-26-2012, 07:15 PM
Yes I understand what you are saying but normally the amount of javascript I use is so small that I don't notice any difference in the loading time for a page no matter where I put the javascript.

Consider when you decide to add more to the JavaScript or add more images and other things to the page. If you put the JavaScript at the bottom to start with then you don't need to move it if it does start to affect ooad times (plus even if you don't notice the difference now someone with a much slower internet connection than you might.

Also the JavaScript itself can run sooner at the bottom as most of the time it can run straight away rather than waiting for everything to load because most of the time you only need the HTML loaded.

Just before the </body> tag is the best place to put most JavaScript so why not get in the habit of putting it there now rather than having to rewrite things later.

Mishu
03-27-2012, 01:38 AM
Consider when you decide to add more to the JavaScript or add more images and other things to the page.

I do and if at some stage it affects loading time significantly then I can move it. But if the javascript never increases that it doesn't matter where I put it since I have a small amount of js

felgall
03-27-2012, 02:09 AM
I do and if at some stage it affects loading time significantly then I can move it. But if the javascript never increases that it doesn't matter where I put it since I have a small amount of js

As I already said - the amount of JavaScript doesn't matter - the other files the page uses can be affected by the presence of even a small script since as soon as the browser sees there's a script to load it will make everything else wait until after whatever is already loading finishes, then it loads the script, then it starts loading other things again.

Consider 100k of image followed by 10 bytes of JavaScript followed by 100k of image followed by 10 bytes of JavaScript followed by 100k of image followed by 10 bytes of JavaScript followed by 100k of image followed by 10 bytes of JavaScript followed by 100k of image followed by 10 bytes of JavaScript followed by 100k of image followed by 10 bytes of JavaScript followed by 100k of image followed by 10 bytes of JavaScript followed by 100k of image. This will take eight times as long to load with the JavaScript there even though the JavaScript is negligible than it would if all the JavaScript were at the end or if there were no JavaScript there at all. The size of the JavaScript isn't what causes this - it is simply the presence of the script.

Browsers can download eight files simultaneously as long as none of them are JavaScript. As soon as you add JavaScript to the queue to download, the browser will wait until the currently downloading files are finished, then download the JavaScript, then start downloading the other files again eight at a time. So the mere presence of the script can delay the start of seven file downloads.

Mishu
03-27-2012, 02:48 AM
As I already said -

Are we going round in circles now? As I said, with the amount of javascript I have I don't notice any difference in the time it takes for my pages to load regardless of where I put my javascript.

felgall
03-27-2012, 03:03 AM
Are we going round in circles now? As I said, with the amount of javascript I have I don't notice any difference in the time it takes for my pages to load regardless of where I put my javascript.

Well if you deliberately want to do it wrong just because it doesn't appear to make any difference to you (but could very well make a huge difference to those using slow dialup connections) then that's your choice.

Mishu
03-27-2012, 03:13 AM
then that's your choice.

Isn't that what I've been saying indirectly all along? :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum