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

guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
jackson
jackson
4 years ago

this perfectly working.

1
0
Would love your thoughts, please comment.x
()
x