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
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Loading JS first or fast at the time opening the website

    I would like to know is there any medthod to load javascript file at the very first when the website on loading like images content etc.. could make the js load before them? or make it load faster

    thanks
    Tommy

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    If you place your external .js file right after ther <body> tag it will load first.
    But of course if the .js file runs on load and references html elements which have not yet loaded then an error occurs. In other words, any JavaScript that affects the way a page displays – either its content or its styling – has to first make sure that everything it requires is there i.e. that the page it's working on is downloaded (and parsed).

    In fact many people recommend placing your JavaScript at the end of your HTML file if possible, right before the closing </body> tag. This allows the majority of page content (like images, tables, text) to be loaded and rendered first. The user sees content loading, so the page looks responsive. At this point, the heavy javascripts can begin loading near the end.

    Only core files that are absolutely needed in the beginning of the page load should be in the <head> section. The rest, like cool menu effects, transitions, etc. can be loaded later. You want the page to appear responsive (i.e., something is loading) up front.

    In any case you need to consider what happens if the user does not have JavaScript enabled.
    Last edited by Philip M; 06-13-2008 at 09:02 AM.

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    or better still
    there is an onload function in HTML for Javascript
    so use
    <script type="text/javascript">
    your javascript
    </script>
    <body onload=" your javascript ">

    this loads the javascript into the page before any HTML

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,460
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Quote Originally Posted by barkermn01 View Post
    or better still
    there is an onload function in HTML for Javascript
    so use
    <script type="text/javascript">
    your javascript
    </script>
    <body onload=" your javascript ">

    this loads the javascript into the page before any HTML
    no is doesn't.
    in fact, just the opposite is true.

    onload fires only after ALL the html and scripts (and images in ff/ie/op) has finished loading.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%


  •  

    Posting Permissions

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