-4

What would more ideal to detect a page protocol so as to access an external file via http. Due to security url's containing http will not work on a page with https. The call is for external JS file. Would it be more ideal to detect this in a JS conditional or go through a server side code to determine this before the page loads.

Thank you in advance.

dar
  • 43
  • 1
  • 6
  • What exactly are you trying to do? Which libraries/files, how are you loading them etc? Please edit your question and provide exact details, and it would be helpful if you want your question answered to include some code as well. – jv-k Sep 17 '15 at 20:36
  • I'm trying to use an external JS file with my site’s JS .ajax({ url: "**http**://external.com/js/external.js", }) I get a console error "The page at 'https://www.example.com' was loaded over HTTPS, but requested an insecure script 'http:/www.external.com/js/externalscript.js'. This request has been blocked; the content must be served over HTTPS." So I'm using if (window.location.protocol != "https:"){ script ="http:.." }else{ script = "https:.." } then say .ajax({ url: script, }) But is it best practice to determine the protocol server side or client side with JS – dar Sep 17 '15 at 20:58
  • Welcome to SO and well done for making that clearer! :) – jv-k Sep 17 '15 at 21:11

1 Answers1

0

You simply omit the http: and turn the URL into a Protocol Relative URL and it will work without any extra code:

.ajax({ url: "//external.com/js/external.js" }) 

More info:

How to Include CSS and JS files via HTTPS when needed?

and here:

https://blog.httpwatch.com/2010/02/10/using-protocol-relative-urls-to-switch-between-http-and-https/

http://billpatrianakos.me/blog/2013/04/18/protocol-relative-urls/

http://support.volusion.com/article/resolving-unsecured-content-secure-pages

Community
  • 1
  • 1
jv-k
  • 618
  • 5
  • 20