Tuesday, February 15, 2011

Protocol-Relative URLs

These are pretty cool. Basically, you don’t specify your scheme when adding a URI to an HTML tag which points to content for your page. Instead, you let the browser determine which protocol was used when requesting the page and, in turn, it uses that protocol for the initial request for additional content. This will typically avoid the dreaded “This page contains both secure and nonsecure items” dialog in IE:



It also simplifies your HTML and JavaScript – you don’t need to know which scheme the browser is using and have stuff like:
var scheme = document.location.protocol;
//some code...
someImage.src = scheme + "//alscodeshop.com/images/pic.png";

Instead, you’ll just have:
someImage.src = "//alscodeshop.com/images/pic.png";

or:
<img src="//alscodeshop.com/images/pic.png" />

By the way, you can use this in all browsers - it’s not IE-specific.

I learned this from Paul Irish (he credits others) and he has a couple of really minor caveats regarding using these...




No comments:

Post a Comment