Skip to content

Instantly share code, notes, and snippets.

@hyrmn
Created April 7, 2011 16:10
Show Gist options
  • Save hyrmn/908100 to your computer and use it in GitHub Desktop.
Save hyrmn/908100 to your computer and use it in GitHub Desktop.
How we include jQuery
<script src="//ajax.googleapis.com/ajax/libs/jquery/${jqueryVersion}/jquery.js" type="text/javascript"></script>
<script type="text/javascript"> !window.jQuery && document.write(unescape('%3Cscript src="' + document.getElementById("rsVirtualRoot").value + 'content/js/jquery/${jqueryVersion}/jquery.min.js" %3E%3C/script%3E'))</script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/${jqueryUiVersion}/jquery-ui.js" type="text/javascript"></script>
<script type="text/javascript"> !window.jQuery.ui && document.write(unescape('%3Cscript src="' + document.getElementById("rsVirtualRoot").value + 'content/js/jqueryui/${jqueryUiVersion}/jquery-ui.min.js" %3E%3C/script%3E'))</script>
@hyrmn
Copy link
Author

hyrmn commented Apr 7, 2011

Basically, the // in the front is a protocol agnostic include. That is, if you're serving the page from http://example.com then it will automatically reference http://ajax.googleapis.com. On the other hand, if you hit https://example.com then it'll automatically go after https://ajax....

Note, this also means that if you load a file locally then the protocol will be file:// and it'll try and hit file://ajax... and you'll wonder why things aren't working.

The following line after both the jQuery and jQuery UI source references simply check to see if the libraries loaded. Google's CDN is extremely reliable but there have been outages. So, if the libraries aren't loaded, we load them from our own server (the noise around virtualroot is how we work with multi-environment deployments).

This gives of the benefit of loading from Google if we can (so many of the big sites do this that we're virtually ensured that it's cached) but then we can load locally if there's any interruption with Google.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment