A matter of protocol

The web is made of sugar, spice and all things nice. On closer inspection, this is what most URLs on the web are made of:



protocol://domain/path



The protocol—e.g. http—followed by a colon and two slashes (for which Sir Tim apologises).
The domain—e.g. adactio.com or huffduffer.com.
The path—e.g. /journal/tags/nerdiness or /js/global.js.


(I'm leaving out the whole messy business of port numbers—which can be appended to the domain with a colon—because just about everything on the web is served over the default port 80.)



Most URLs on the web are either written in full as absolute URLs:



a href="http://adactio.com/journal/tags/nerdi..."
script src="http://huffduffer.com/js/global.js"


Or else they're written out relative to the domain, like this:



a href="http://adactio.com/journal/tags/nerdi..."
script src="http://adactio.com/js/global.js"


It turns out that URLs can not only be written relative to the linking document's domain, but they can also be written relative to the linking document's protocol:



a href="http://adactio.com//adactio.com/journ..."
script src="http://adactio.com//huffduffer.com/js..."


If the linking document is being served over HTTP, then those URLs will point to http://adactio.com/journal/tags/nerdi... and http://huffduffer.com/js/global.js but if the linking document is being served over HTTP Secure, the URLs resolve to https://adactio.com/journal/tags/nerdiness and https://huffduffer.com/js/global.js.



Writing the src attribute relative to the linking document's protocol is something that Remy is already doing with his HTML5 shim:






If you have a site that is served over both HTTP and HTTPS, and you're linking to a CDN-hosted JavaScript library—something I highly recommend—then you should probably get in the habit of writing protocol-relative URLs:






This is something that HTML5 Boilerplate does by default. HTML5 Boilerplate really is a great collection of fantastically useful tips and tricks …all wrapped in a terrible, terrible name.





Tagged with
urls
nerdiness
linking
protocol

 •  0 comments  •  flag
Share on Twitter
Published on August 20, 2011 18:47
No comments have been added yet.


Jeremy Keith's Blog

Jeremy Keith
Jeremy Keith isn't a Goodreads Author (yet), but they do have a blog, so here are some recent posts imported from their feed.
Follow Jeremy Keith's blog with rss.