Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is there no protocol specified for this script tag

With this I can generate a like button :D

<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

but why is it not

js.src = "some protocol://connect.facebook.net/en_US/all.js#xfbml=1";

Why no specific the protocol?

like image 654
Ser1 Avatar asked Feb 12 '13 12:02

Ser1


2 Answers

That’s a helpful trick which allows you to use a single reference that works on both HTTP and HTTPS pages. When a URL’s protocol is omitted, the browser uses the underlying document’s protocol instead.

On a page loaded through regular, unencrypted HTTP, script references using that URL will be loaded via HTTP and be cached as normal. Likewise, on a secure page that was loaded via HTTPS.

Thus, using the protocol-less URL allows a single script reference to adapt itself to what’s most optimal: HTTP and it’s full caching support on HTTP pages, and HTTPS on secured pages so that your users aren’t confronted with a mixed content warning.

Source: http://encosia.com/cripple-the-google-cdns-caching-with-a-single-character/

like image 132
hyperbeam22 Avatar answered Nov 13 '22 14:11

hyperbeam22


It's a little trick called protocol-relative URL that "save you some headaches".

like image 40
ZER0 Avatar answered Nov 13 '22 12:11

ZER0