Browser support for URLs beginning with double slash

25,695

Solution 1

This behavior was part of RFC 1808 (Section 4) which is about 16 years old, so every major browser should (and does) support this.

Sadly, there's a bug with IE7 and -8 that will make them download the resources twice if a protocol-relative URL is used on a link or @import - which shouldn't be a big problem, but is ugly and should be kept in mind.

Solution 2

If you are developing on a local machine there's possibility it will fail with src="file://host.com/filename".

In this situation you need to specify scheme explicitly: http://host.com/filename or https://host.com/filename.

Share:
25,695

Related videos on Youtube

dtbarne
Author by

dtbarne

Self taught web developer and entrepreneur with 10+ years of experience.

Updated on February 26, 2020

Comments

  • dtbarne
    dtbarne over 4 years

    I've recently seen a few links used without a protocol. It didn't seem too difficult to understand - I think it's a great idea and pretty intuitive.

    For those of you unaware, using a URL like //example.com/script.js will point to either http://example.com/script.js or https://example.com/script.js depending on whether or not the URL originates from a http or https URL. Including http scripts or images from a https page can be a security concern, for example, so this solves that without the need for protocol detection in your code.

    My question is, what sort of browser/OS support is there for it? Is it safe to use in production? It would certainly make things a bit easier.

    Simple example and test: http://codetester.org/916c6916

    EDIT: Just a follow up that I've been using this for my company's ad server in production for many things without issue for a couple years now.

    • Keith
      Keith almost 13 years
      Answered here: stackoverflow.com/questions/4659345/… --- In short, it's in the RFC specification so it should be supported by all major browsers.
    • dtbarne
      dtbarne almost 13 years
      @Keith Thanks, that's good and all, but looking through the RFC specs, I don't actually see anything about this beginning double slash thing. Was hoping to find some definitive browser testing already done. :)
  • TwystO
    TwystO over 6 years
    This is an old post, but I also wanted to say it's not a good practice at all. Recently I had some trouble on one of our customers admin dashboard. The URL //code.jquery.com/jquery-2.1.3.min.js was not found and the library not loaded. I then try to load http://code.jquery.com/jquery-2.1.3.min.js and see an error page. I suspect a proxy or VPN or firewall security configuration they set internally. After updating the URL to use the https protocol, everything works fine.
  • Tim Anthony
    Tim Anthony over 3 years
    When developing offline you should use a local server anyway, which would make the URL "localhost".