0

For instance, I am confused with understanding "//" from "//unpkg.com/leaflet@0.7.7/dist/leaflet.js"and "/" from "/js/ui-leaflet.min.js"

Cascara
  • 83
  • 1
  • 3

1 Answers1

1

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

William Isted
  • 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
  • 1
    Browsers 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