Detect SSL/HTTPS using JavaScript

The below code shows you how to detect if a website is using SSL/HTTPS or not in JavaScript.

To do this, we simply look at document.location.protocol property. If it returns https:, your website is using SSL/HTTPS and vice versa.

The code looks simple as this:

ssl-detection.js
function detectSSL() {
    return (document.location.protocol === 'https:');
}

 

Use the function:

if (detectSSL()) {
    console.log('SSL');
} else {
    console.log('NON-SSL');
}