Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect HTTPS with JavaScript [duplicate]

I am trying to find how can I detect with JavaScript if I am in a HTTP or HTTPS environment.

I am calling an Ajax request so if I am in HTTPS and call HTTP Ajax then I get a 302 Moved Temporarily.

I was thinking of getting the current window.location.href and do a string manipulation.

What is the best way of detecting HTTPS using JavaScript?

like image 221
ntan Avatar asked May 18 '10 08:05

ntan


1 Answers

Looking at how google analytics add their script to the page:

ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 

Then document.location.protocol would seem safe for all browsers.

like image 199
Rich Avatar answered Sep 20 '22 03:09

Rich