5 ms·
The // prefix is actually used for protocol-relative URLs. So //google.com leads to https://google.com https://google.com on a page with SSL, and it leads to ht
by nbpoole 15y ago
The // prefix is actually used for protocol-relative URLs. So //google.com leads to https://google.com https://google.com on a page with SSL, and it leads to http://google.com http://google.com on an unencrypted page.
See:
http://nedbatchelder.com/blog/200710.html#e20071017T215538 http://nedbatchelder.com/blog/200710.html#e20071017T215538
http://paulirish.com/2010/the-protocol-relative-url/ http://paulirish.com/2010/the-protocol-relative-url/
- astrodust 15y agoDoesn't seem like it would be that abusive for it to end up being "auto-detect" when there is nothing to be relative to. A "//" link in an HTTPS site would be required to be the same protocol anyway. It's only the spdy->http fallback that could prove to be a nuisance.
- rufibarbatus 15y agoConversely, the protocol bit is relevant whenever your link is not domain- or protocol-relative, or when you can't be sure. And you can't be sure. Here's why: if someone saves your page on their hard drive and you're using protocol-relative URLs, the implied "http" just became "file". (Which in fact may be a nice excuse for some clever hacks.) Test it yourself! Save the following snippet to your computer and see how your browser resolves the URL: <!DOCTYPE html> <title>Testing 1 2 3</title> <p><a href="//google.com/">Linky</a></p>
- sjs 15y agoYou're assuming people care about that use case. I think in most cases it's irrelevant.
- Vivtek 15y agoOh man. Are you really saying that corner cases are a priori negligible? That seems like a dangerous road to start down when you're discussing basic protocols!
- sjs 15y agoIf your use case entails people saving your site to disk then you have may not be able to use // for protocol relative links, but most people don't have to worry about such things. I'm saying that those who need to worry about such things should and the rest of us should not. Nothing more and nothing less.
- rawsyntax 15y agowget has switches to replace http with file when saving a website to disk
- teyc 15y agoHTML Base tag is your friend. http://www.w3schools.com/TAGS/tag_base.asp http://www.w3schools.com/TAGS/tag_base.asp
- sleepyhead 15y agoW3Schools is not your friend. http://w3fools.com/ http://w3fools.com/
- rufibarbatus 15y agoAnd here it is in the specification: http://dev.w3.org/html5/spec/Overview.html#the-base-element http://dev.w3.org/html5/spec/Overview.html#the-base-element And accordingly, adding `<base href="http://example.com/ http://example.com/ >` to that test above seems to work well on modern browsers.
- nyellin 15y agoCool, I didn't know that. // has another meaning in XML XPath, a language for navigating XML documents. // selects nodes anywhere in a document. For example, //p would select all paragraphs in an XHTML page.
- jonursenbach 15y agoThe problem with this is that browsers that are older than IE8 don't support it, so if you're still targeting those users you're SOL on this method.
- jonhohle 15y agoI've used this technique since 2006 or so with IE6. The only concern I know of relates to using odd port numbers without a protocol in IE (but I don't think this is a concern if the protocol is relative). At the time, every other browser tested worked as well (not that that was much of a shock). I don't have a copy of IE6 to test on at the moment, but what makes you think IE8 is required for relative protocols?
- ryanpetrich 15y agoIE6 will also fetch some resources twice if they are protocol-relative URLs
- justincormack 15y agoThat doesn't wound like a shopstopper... optimising performance for IE6 is kind of pointless..