Detect HTTPS with JavaScript

https, javascript

Solution

You can use the non-standard

window.location.protocol 

In Firefox: MDC documentation

In IE, it seems to be

 document.location.protocol

MSDN documentation

I can't find reliable info on how this behaves on other browsers, but I expect they adhere to the quasi-standard of `document.location.protocol`.

Maybe the jQuery url plugin sorts this out without having to deal with cross-browser differences - I've never used it myself, but it looks promising:

jQuery.url.attr("protocol");

Problem

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?

Original source

Related problems