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');
}

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
jackson Recent comment authors
newest oldest most voted
jackson
Guest
jackson

this perfectly working.