Download javascript dynamically based on http or https protocol

I have a javascript file located on another server and I include the javascript file on the http page, for example

 <script type="text/javascript" src="http://www.mydomain.com/scriptfile.js"> 

or more https for example

 <script type="text/javascript" src="https://www.mydomain.com/scriptfile.js"> 

the problem is that I have a page switch from http to https , for example if the user is in

http://www.mysite.com/home (url home home) and I download javascript on top of http , now that the user is navigating to another page, for example https://www.mysite.com/transaction (URL- website transaction address), I download the script on top of https and it works fine. from the specified transaction URL, if the user clicks on https://www.mysite.com/home (the home url is changed to https), the script I downloaded over http fails due to unsecured content. Any approval for this is appreciated.

+4
source share
2 answers

Protocol is optional. If you omit it, the browser will use any document protocol. So you can do:

 <script type="text/javascript" src="//www.mydomain.com/scriptfile.js"> 

The correct protocol will be used.

+10
source

Use the protocol related URL for your script:

 <script type="text/javascript" src="//www.mydomain.com/scriptfile.js"> 

This will use the same protocol as the calling page.

+5
source

All Articles