Tuesday, February 28, 2017

How To Detect HTTP Or HTTPS in JavaScript?

You can use location.protocol to detect the protocol that the site is using. Lets say we have the url https://www.example.com. We can use following code to detect if it has HTTP or HTTPS.
//output => https:

You can change the protocol of the site simply by using following logic:
var protocol = location.protocol === 'https:' ? 'http' : 'https';

//output => http

No comments:

Post a Comment