2

Is it possible to automatically detect whether or not the page is accessed via http or https and then include a https://url.js or http://url.js ?

Is javascript included via https any different? I'm talking about cache age and so on?

Thanks, Wesley

1 Answers1

11

Use a protocol-relative link, starting with // instead of http or https:

<script type='text/javascript' src="//path/to/file.js"></script>
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • Is that supported in every browser? Interesting. –  Nov 05 '11 at 20:19
  • @Wesley I believe so. I think it's been part of the spec since the mid 1990's and am trying to find the W3 documentation now. – Michael Berkowski Nov 05 '11 at 20:20
  • This question has a bit of interesting additional information about protocol-relative URL's when used in HTML email http://stackoverflow.com/questions/4303633/preventing-secure-insecure-errors-by-using-protocol-relative-urls-for-image-sour – Michael Berkowski Nov 05 '11 at 20:37
  • width domain name will be like `//domain.net/path/to.file.js` – Vladimir Ch Sep 13 '21 at 13:47