SharePoint and protocol-relative URLs

The Google Hosted Libraries are awesome — very fast CDNs that serve common development libraries like jQuery at blaziing speed. And upgrading to the newest version of a library is as simple as changing a URL.

You can use the Google libraries in SharePoint, but SharePoint Designer has a quirk that will trip you up if you’re  not careful.

Google’s suggested link looks like this:

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Note that there’s no “http”  or “https” in the “src” line.  THis is known as a protocol-relative URL. In modern browsers, it will serve “http” content to http requests, and “https” content to https requests.

The problem

Cool, huh? Yep, sure is.

And if you open a SharePoint master page in Designer, add the code, and then save it, everything works great.

Until the NEXT time you edit and save that master page. Upon saving, it will truncate the “src” of your script to this:

<script src="/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Now your site will think it’s a reference to a local file, fail to find it, and all your jQuery code will stop working.

This has been a problem for years, and hasn’t been fixed by successive versions of SharePoint.

The workaround

It’s fairly simple to work around the problem. Simply include either “http” or “https” in the src:

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

Now SharePoint will leave the URL alone, and your code will work. Downside is that you have to choose either “http” or “https” — you lose the flexibility of the protocol-relative URL, but otherwise it’s fine.