Archive for the ‘javascript’ Category

Detecting browser and protocol in Javascript

<script type="text/javascript">   if(/chrom(e|ium)/.test(navigator.userAgent.toLowerCase()) && location.protocol==’http:’) { alert(’Using current position in chrome restricted under http protocol’) } else if (/msie/.test(navigator.userAgent.toLowerCase())) { console.log("This browser is MSIE"); } else if (/firefox/.test(navigator.userAgent.toLowerCase())) { console.log("This browser is Firefox"); }   </script>

how-to get current hostname in javascript

window.location.host or document.location.host : you’ll get sub.domain.com:8080 or sub.domain.com:80 window.location.hostname or document.location.hostname: you’ll get sub.domain.com window.location.protocol or document.location.protocol: you’ll get http: window.location.port or document.location.port: you’ll get 8080 or 80 window.location.origin or document.location.origin: you’ll get http://sub.domain.com *