For instance, I am confused with understanding "//" from "//unpkg.com/leaflet@0.7.7/dist/leaflet.js"and "/" from "/js/ui-leaflet.min.js"
-
3`//` is `http://` or `https://` dependent on the current page schema, whilst `/` is just an absoute path – Lawrence Cherone Nov 25 '19 at 17:43
-
To add to the comment above, "`//` is `http://` or `https://`"... Which one is chosen depends on the scheme (http/https) of the containing page. – spender Nov 25 '19 at 17:47
-
https://en.wikipedia.org/wiki/URL#Protocol-relative_URLs – epascarello Nov 25 '19 at 18:00
1 Answers
The //
at the start of a resource url is an implicit protocol which tells the browser to use the same protocol as the current page i.e. http
or https
.
If you are on a page at https://www.example.org a script src of //unpkg.com/leaflet@0.7.7/dist/leaflet.js
will load https://unpkg.com/leaflet@0.7.7/dist/leaflet.js
.
If you are on a page at http://www.example.org a script src of //unpkg.com/leaflet@0.7.7/dist/leaflet.js
will load http://unpkg.com/leaflet@0.7.7/dist/leaflet.js
.
A script src of /unpkg.com/leaflet@0.7.7/dist/leaflet.js
would mean the browser loads the script from the implicit domain name i.e. http://www.example.org
which would translate to http://www.example.org/unpkg.com/leaflet@0.7.7/dist/leaflet.js
. If used incorrectly this can stop external resources loading. This should be used for resources located at the same hostname.
You can read more about URLs at developer.mozilla.org

- 11,641
- 4
- 30
- 45
-
Is there a difference in how the browser interprets and handles this? Wouldn't both maintain the same scheme + host + port as the containing page? – Nicholas Hirras Nov 25 '19 at 17:51
-
1Browsers should handle this as mentioned above. So the browser would not change the scheme between `http` and `https` unless the destination performs a redirect. – William Isted Nov 25 '19 at 18:01