Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 5 of 5
  1. #1
    Senior Member RDesignista's Avatar
    Join Date
    Feb 2012
    Location
    Coconut Tree City
    Posts
    822
    Member #
    30921
    Liked
    123 times
    I was wondering... what is the proper way to be adding jquery/js to a document? Currently, I'm doing this setup where I include the file (functions.js) right before the footer (I started doing this because I reverse engineered a website and just copied what they were doing). Then in functions.js, I just add

    Code:
    jQuery(function($) {
      //jquery functions go here
    }
    I think js/jquery loads faster this way than if you include the js right in the document, am I right?

    Another way I've seen is that people do some

    Code:
    document.ready(function() {
     
    } );
    or

    Code:
    window.load
    Sometimes it's in the <head>, sometimes it's above </body>.

    Can anyone tell me if there is a right way or wrong way? For example, putting <script> right in the middle of <body> might not be considered right. I'm asking this because I'm not 100% sure what exactly I'm asking and how to describe it using a google search query. Thanks in advance, if you can decipher my gibberish.

  2.  

  3. #2
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,483
    Member #
    425
    Liked
    2783 times
    Yes, Javascript (not just jQuery) is best placed in its own file. It's better organized, and it can be cached by browsers and services such as CloudFlare (if you go that way).

    Personally, I don't think there is a general right or a wrong way. Depends on what you're trying to accomplish. I have some functions that need to be called right after the <body> tag for rendering reasons, some that can wait until load, and some in all points in between.
    Ronald Roe likes this.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  4. #3
    Senior Member RDesignista's Avatar
    Join Date
    Feb 2012
    Location
    Coconut Tree City
    Posts
    822
    Member #
    30921
    Liked
    123 times
    Great, thanks for that. I'm just trying to learn more and more about appropriate ways to approach js and jquery so that you don't have conflicts or mis-placed scripts and cause the entire site to slow down or require more resources than it would need if it were organized properly.

    The idea of making it a separate file so that you can cache it or put it on a cdn is a great idea as well.

  5. #4
    Senior Member Andrew Yurlov's Avatar
    Join Date
    Apr 2011
    Location
    Portland, OR
    Posts
    1,306
    Member #
    27566
    Liked
    181 times
    Personally I alwasy do it this way

    Code:
    $document.ready(function(){
     //code here
    )};
    Artificial intelligence is nothing compared to natural stupidity -Someone

    Design is a funny word. Some people think design means how it looks.
    But of course, if you dig deeper, itís really how it works.
    -Steve Jobs



  6. #5
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    I use a separate file, usually placed at the end.

    Code:
    $(function(){
    //Code and what-not
    });
    IIRC, this assumes "document.ready".
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 05:58 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com