SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript browser recognition + redirect. Help!

    Hello ,

    I have a javascript that recognises the browser and then redirects to a page. However , I want only IE explorer to be redirected. I mean , if browser is IE then redirect to a page.
    Unfortunately , it redirects Opera too! Here's the code:

    Code:
        <SCRIPT language="JavaScript">
        <!--
        var browserName=navigator.appName;
        var browserVer=parseInt(navigator.appVersion);
        if ((browserName=="Netscape" && browserVer>=3))
          version="n3";
        else
          version="n2";
    
        if (version=="n3")
          ;
        else
          window.location = "www.google.com"
        //-->
        </SCRIPT>

    Can anybody help me?

  2. #2
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *bump* help please

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any user can cause any browser to send any string in the navigator.appName.
    Opera is the easiest to set, and early versions sent an IE string by default, if IE was already installed.

    You can use object detection to identify IE, but browser support changes constantly, so you have to keep up with any changes.

    <script type="javascript">
    navigator.isIE= !!(!window.addEventListener && window.attachEvent);

    if(navigator.isIE)// do IE only code
    else// do non IE
    </script>

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,603
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Checking the useragent means you redirect anyone who has the useragent set to that value and since IE and Firefox allow the useragent to be set to anything and other browsers allow it to at least be set to the IE or Firefox default value you can't use useragent to distinguish between browsers.

    You can distinguish Internet Explorer from other browsers because it runs JScript instead of JavaScript and so recognises JScript conditional comments.

    The following code will run in IE only as all other browsers see it as a comment.

    Code:
    /*@cc_on
       @if (@_jscript)
          window.location = "www.google.com";
    @end @*/
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your replies!

    Felgall , it works like a charm Thak you very much!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •