Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Coder
    Join Date
    Mar 2011
    Posts
    164
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Can <body onload> be modified?

    What I mean is that my <body> tag currently is like this:

    Code:
    <body class="page-course">
    I would like to insert a pop-up contact form which requires the body tag to be like this:

    Code:
    <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!!

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Code:
    <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>

  • #3
    Regular Coder
    Join Date
    Mar 2011
    Posts
    164
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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



    Quote Originally Posted by DaveyErwin View Post
    Code:
    <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>

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by listerdl View Post
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by felgall View Post
    (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.
    Last edited by Mishu; 03-25-2012 at 12:01 AM.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Mishu View Post
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by felgall View Post
    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.

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Mishu View Post
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by felgall View Post
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #10
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by felgall View Post
    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.

  • #11
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Mishu View Post
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #12
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by felgall View Post
    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

  • #13
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Mishu View Post
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #14
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by felgall View Post
    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.

  • #15
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Mishu View Post
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •