Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
126
rated 0 times [  132] [ 6]  / answers: 1 / hits: 39050  / 14 Years ago, tue, may 18, 2010, 12:00:00

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?


More From » https

 Answers
39

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

[#96758] Friday, May 14, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anitamaeg

Total Points: 466
Total Questions: 106
Total Answers: 106

Location: Suriname
Member since Sun, Jun 13, 2021
3 Years ago
;