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.
console.log(location.protocol);
//output => https:

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

console.log(protocol);
//output => http

Comments

Popular Posts