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.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Apr 2010
    Posts
    125
    Thanks
    6
    Thanked 0 Times in 0 Posts

    How do you get a script to run BEFORE the page finishes loading?

    I have a CSS/Java popup which uses onload="popup('imageGet')" in the <body> tag which calls an external .js file.

    Obviously, it doesn't fire until the whole page has loaded but it is a message I want people to see before they start navigating the site.

    How to I make it fire when the page is called - i.e the rest of the page can finish downloading behind it?

    Thanks
    Mike

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,239
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    Place the call to the external .js file in the <head> section,
    <script type = "text/javascript" src = "myjsfile.js"></script>
    but of course any HTML elements referenced must exist at the moment the script is called. Probably that is why the script is called onload.

    If the script refers to HTML elements, try placing the call right after the location where they are defined.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 02-05-2013 at 05:14 PM. Reason: typo

    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.

  • #3
    Regular Coder
    Join Date
    Apr 2010
    Posts
    125
    Thanks
    6
    Thanked 0 Times in 0 Posts
    I have the external js file already in the header. But I have to toggle the css popup. So the script has to be "fired" initially and the resulting popup has an X on it to let people close the message.

    That's why I'm currently working with onload="popup('imageGet')" to get the popup to generate, and there there is an onclick="popup('imageGet')" in the popup which closes it.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    To get a script to run as early as possible place the script tag immediately after the tag it references.

    In practical terms there will seldom be a noticeable difference if you simply place the script at the recommended spot just before the </body> tag.

    The only time onload is needed is where you require all the images to have loaded before the script can run - such as in a script that substitutes a locally hosted image for any remote images that fail to load.

    The only scripts that should go in the head are those that potentially redirect to another page prior to loading the current page.
    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.


  •  

    Posting Permissions

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