Coder Perfect

A url/src/href attribute with two forward slashes [duplicate]

Problem

I was reading over the HTML5 Reset source code when I came across the following line:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>

Why are there two forward slashes at the beginning of the URL? Is this a shortened version of http://?

Asked by Michael Parkin

Solution #1

They’re also known as “protocol relative URLs,” and they’re handy when items — like the JS file in your example — can be delivered and/or requested in either a http or https context. You can avoid implementing protocol relative URLs by utilizing protocol relative URLs.

if (window.location.protocol === 'http:') {
    myResourceUrl = 'http://example.com/my-resource.js';
} else {
    myResourceUrl = 'https://example.com/my-resource.js';
}

type of logic strewn throughout your codebase (assuming, of course, that the server at example.com is able to serve content through both http and https).

The Magento 1.X E-Commerce engine is a good real-world example: the category and product pages use plain http by default for performance reasons, but the checkout uses https.

If some resources (for example, promotional banners in the site’s header) are referred using non-protocol relative URLs (i.e. http://example.com/banner.jpg), clients arriving at the https enabled checkout are met with an unwelcoming message.

prompt – which, as one could expect, isn’t ideal for business.

However, if the aforementioned resource is referenced via /example.com/banner.jpg, the browser will load it using the appropriate protocol on both plain http product/category pages and the https-enabled checkout flow.

tl;dr: If there’s even a remote chance of a mixed http/https environment, merely reference resources with the double slash/protocol relative URLs — presuming the host serving them supports both http and https.

Answered by vzwick

Solution #2

Depending on how the request was made, it will automatically add https or http.

Answered by Radu Cugut

Post is based on https://stackoverflow.com/questions/9646407/two-forward-slashes-in-a-url-src-href-attribute