Question

Recently I've seen these kinds of URLs in CDN documents:

//cloudflare.cdnjs.com/foo

And I realised that they lacked the http: or https: in front of them.

Hmm, I thought, and tried implementing them in a local test, and realised that they didn't work (they resolved // to be file://, though it was supposed to be http:).

Then when I uploaded this to my testing server, it worked (resolved // to be http://).

So I was wondering, why would anybody write it without the protocol?

Thanks.

Was it helpful?

Solution

(Technically, it's not the "protocol", but the URI scheme)

For HTTP, excluding the scheme means that the UA will use the current scheme (and therefore protocol) to retrieve the file, which means some copy + pasted code can be re-used for both HTTP and HTTPS (or even SPDY) requests without triggering the "Partially secure page" warnings in browsers.

OTHER TIPS

Depending on the user's security settings, browsers will often present a warning if a page serves up mixed content (both HTTP and HTTPS). Of course, you could just code your HTML using the same protocol that you intend the page to be hosted with. However, it's not uncommon to use the same HTML in both secure and non-secure portals. Furthermore, you'd really prefer your HTML to be agnostic to the underlying protocol.

Writing CDN links like this makes sure that the CDN request uses the same protocol as the host page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top