links without http: like //code.jquery.com/etc [cl

2019-04-09 01:05发布

问题:

Lately I've noticed that some sites (such as jsFiddle) and some widgets such pinterest feed widget, and many others.

They started to use links in the format

  • //code.jquery.com/jquery-1.9.1.js
  • //assets.pinterest.com/js/pinit.js

    1. what does the // means ?
    2. is it the same as http ?
    3. if // is the same as http why using it instead of http ?
    4. if both are different, can I use it to link my website ?

as addition: I'm not sure, but I think I've seem some use //name:sub.domaine.com, am I wrong ? if those exist, what do they mean ?

Edit:

as explained in an answer bellow, I can use the // notation to make my links shorter, but when I use it for my website: Chrome changes the link to: file:/// which is not what I expected

回答1:

This is a protocol-relative URL. If the page which includes it uses HTTP, then it's HTTP. If the page which includes it uses HTTPS, then it uses HTTPS.

This is convenient so that you can ensure that you don't fetch insecure resources in a secure page (this causes the "mixed content" warning you might have seen), without bothering with the overhead of SSL/TLS in a page which is delivered unencrypted anyhow.

It's similar to how URLs beginning with a single / are resolved relative to the current protocol and hostname, and URLs with no leading / or scheme are resolved relative to the current page's directory.



回答2:

To keep it simple: most web browsers now do not require the full [http://www] anymore. So yes, to be cool and to keep the URL's short, you can skip the full [http://www.] and just use //