Detect browser and redirect using javascript

Last updated on May 18th, 2016 at 04:54 pm

Click to rate this tutorial!
[Total: 8 Average: 4.6]

Very simple tutorial using javascript to detect browser and redirect to specific webpage using javascript. Browsers detected are Internet explorer, Chrome or Mozilla Firefox
Detecting Internet Explorer

Detecting Internet Explorer is easy, as the user agent string – available in navigator.userAgent – will always include the text “MSIE”. We can then check for this in a simple indexOf:

<script type="text/javascript">
    if (navigator.userAgent.indexOf('MSIE') !=-1)
    {
    	// Using IE
window.location = "http://mywebsite.com/iewebpage"
    }
</script>

Detecting Firefox

Thankfully, Firefox places a “Firefox” in its user agent string that is reliable for detecting it:

<script type="text/javascript">
    if (navigator.userAgent.indexOf('Firefox') !=-1)
    {
    	// Using Firefo
window.location = "http://mywebsite.com/firefoxwebpage"

    }</script>

Detecting Google Chrome

Detecting Chrome is easy, as the user agent string – available in navigator.userAgent – will always include the text “Chrome”. We can then check for this in a simple indexOf:

<script type="text/javascript">
    if (navigator.userAgent.indexOf('Chrome') !=-1)
    {
    	// Using Chrome
window.location = "http://mywebsite.com/chromewebpage"
    }
</script>
Click to rate this tutorial!
[Total: 8 Average: 4.6]

Leave a Reply

Your email address will not be published.