简体   繁体   中英

How to load javascript libraries securely

I apologize if this question is simplistic, beginning web developer here.

I have a page that I am serving securely as https. The page uses the following two libraries:

<script src="http://myjs.us/param.js"></script>
<script src="http://myjs.us/entify.js"></script>

I am getting errors of the following type:

[blocked] The page at ... was loaded over HTTPS, but ran insecure content from ' http://myjs.us/param.js ': this content should also be loaded over HTTPS.

So I get why I am getting this error, it is because I am loading the javascript libraries from an unsecure source. My question is where can I get these from a secure source?

Thanks in advance.

The basic solution is to remove the protocol form the URL when you call the javascript, change

<script src="http://myjs.us/param.js"></script>

to this

<script src="//myjs.us/param.js"></script>

With this you ensure that the javascript will load with the same protocol of the entire page.

Be sure that the server supports https (myjs.us for you), otherwise you will get an error like failed to load resource... , In this case, maybe you want to use a CDN with https support, like cdnjs

You can omit the protocol in your URLs:

<script src="//myjs.us/param.js"></script>
<script src="//myjs.us/entify.js"></script>

The browser will default to the current protocol being used by the page, in this case https . Of course, if myjs.us doesn't support https then that would be another issue entirely, and one you can't really solve from your page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM