Coder Perfect

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


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

<script src="//"></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 = '';
} else {
    myResourceUrl = '';

type of logic strewn throughout your codebase (assuming, of course, that the server at 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., 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 /, 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