Where do you include the jQuery library from? Google JSAPI? CDN?

后端 未结 16 1611
攒了一身酷
攒了一身酷 2020-11-22 09:21

There are a few ways to include jQuery and jQuery UI and I\'m wondering what people are using?

  • Google JSAPI
  • jQuery\'s site
  • your own site/serv
相关标签:
16条回答
  • 2020-11-22 10:01

    Without a doubt I choose to have JQuery served by Google API servers. I didn't go with the jsapi method since I don't leverage any other Google API's, however if that ever changed then I would consider it...

    First: The Google api servers are distributed across the world instead of my single server location: Closer servers usually means faster response times for the visitor.

    Second: Many people choose to have JQuery hosted on Google, so when a visitor comes to my site they may already have the JQuery script in their local cache. Pre-cached content usually means faster load times for the visitor.

    Third: My web hosting company charges me for the bandwidth used. No sense consuming 18k per user session if the visitor can get the same file elsewhere.

    I understand that I place a portion of trust on Google to serve the correct script file, and to be online and available. Up to this point I haven't been disappointed with using Google and will continue this configuration until it makes sense not to.

    One thing worth pointing out... If you have a mixture of secure and insecure pages on your site you might want to dynamically change the Google source to avoid the usual warning you see when loading insecure content in a secure page:

    Here's what I came up with:

    <script type="text/javascript">
        document.write([
            "\<script src='",
            ("https:" == document.location.protocol) ? "https://" : "http://",
            "ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js' type='text/javascript'>\<\/script>" 
        ].join(''));
    </script>
    

    UPDATE 9/8/2010 - Some suggestions have been made to reduce the complexity of the code by removing the HTTP and HTTPS and simply use the following syntax:

    <script type="text/javascript">
        document.write("\<script src='//ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js' type='text/javascript'>\<\/script>");
    </script>
    

    In addition you could also change the url to reflect the jQuery major number if you wanted to make sure that the latest Major version of the jQuery libraries were loaded:

    <script type="text/javascript">
        document.write("\<script src='//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript'>\<\/script>");
    </script>
    

    Finally, if you don't want to use Google and would prefer jQuery you could use the following source path (keep in mind that jQuery doesn't support SSL connections):

    <script type="text/javascript">
        document.write("\<script src='http://code.jquery.com/jquery-latest.min.js' type='text/javascript'>\<\/script>");
    </script>
    
    0 讨论(0)
  • 2020-11-22 10:01

    In addition to people who advices to host it on own server, I'd proposed to keep it on separate domain (e.g. static.website.com) to allow browsers to load it into separate from other content thread. This tip also works for all static stuff, say images and css.

    0 讨论(0)
  • 2020-11-22 10:01

    I will add this as a reason to locally host these files.

    Recently a node in Southern California on TWC has not been able to resolve the ajax.googleapis.com domain (for users with IPv4) only so we are not getting the external files. This has been intermittant up until yesterday (now it is persistant.) Because it was intermittant, I was having tons of problems troubleshooting SaaS user issues. Spent countless hours trying to track why some users were having no issues with the software, and others were tanking. In my usual debugging process I'm not in the habit of asking a user if they have IPv6 turned off.

    I stumbled on the issue because I myself was using this particular "route" to the file and also am using only IPV4. I discovered the issue with developers tools telling me jquery wasn't loading, then started doing traceroutes etc... to find the real issue.

    After this, I will most likely never go back to externally hosted files because: google doesn't have to go down for this to become a problem, and... any one of these nodes can be compromised with DNS hijacking and deliver malicious js instead of the actual file. Always thought I was safe in that a google domain would never go down, now I know any node in between a user and the host can be a fail point.

    0 讨论(0)
  • 2020-11-22 10:06

    If you want to use Google, the direct link may be more responsive. Each library has the path listed for the direct file. This is the jQuery path

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
    

    Just reread your question, is there a reason your are using https? This is the script tag Google lists in their example

    <script src="http://www.google.com/jsapi"></script>
    
    0 讨论(0)
  • 2020-11-22 10:07

    Pros: Host on Google has benefits

    • Probably faster (their servers are more optimised)
    • They handle the caching correctly - 1 year (we struggle to be allowed to make the changes to get the headers right on our servers)
    • Users who have already had a link to the Google-hosted version on another domain already have the file in their cache

    Cons:

    • Some browsers may see it as XSS cross-domain and disallow the file.
    • Particularly users running the NoScript plugin for Firefox

    I wonder if you can INCLUDE from Google, and then check the presence of some Global variable, or somesuch, and if absence load from your server?

    0 讨论(0)
  • 2020-11-22 10:08

    I just include the latest version from the jQuery site: http://code.jquery.com/jquery-latest.pack.js It suits my needs and I never have to worry about updating.

    EDIT:For a major web app, certainly control it; download it and serve it yourself. But for my personal site, I could not care less. Things don't magically disappear, they are usually deprecated first. I keep up with it enough to know what to change for future releases.

    0 讨论(0)
提交回复
热议问题