-1

I am trying to redirect users to certain pages depending on which browser they are using, specifically if IE, then go to this page, else any other browser this page.. I had a JavaScript function that was working fine, but after IE10/IE11 came out, it no longer works. Using others codes combined, I came up with this:

function get_browser()
{
    var N=navigator.appName, ua=navigator.userAgent, tem;
    var M=ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);

    if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
    M=M? [M[1], M[2]]: [N, navigator.appVersion, '-?'];

    return M[0];
}

function get_browser_version()
{
    var N=navigator.appName, ua=navigator.userAgent, tem;
    var M=ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);

    if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
    M=M? [M[1], M[2]]: [N, navigator.appVersion, '-?'];

    return M[1];
}

var browser=get_browser();
var browser_version=get_browser_version();

if ((browser=="msie")
&& (version>=4))
{
    if(browser=="opera"||"chrome"||"safari"||"firefox") {
        location.replace("mobile_demo.php"); }
    else { location.replace("full_demo.php"); }
}

However it is not working. Any help is appreciated. Thanks!

Correct code thanks to Pointy's help:

 function get_browser()
 {
     var N=navigator.appName, ua=navigator.userAgent, tem;
     var M=ua.match(/(opera|chrome|safari|firefox|msie|trident)\/?\s*(\.?\d+(\.\d+)*)/i);

     if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
     M=M? [M[1], M[2]]: [N, navigator.appVersion, '-?'];

     return M[0];
 }
 //Optional to get browser version, not needed in this case
 function get_browser_version()
 {
     var N=navigator.appName, ua=navigator.userAgent, tem;
     var M=ua.match(/(opera|chrome|safari|firefox|msie|trident)\/?\s*(\.?\d+(\.\d+)*)/i);

     if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
     M=M? [M[1], M[2]]: [N, navigator.appVersion, '-?'];

     return M[1];
 }

 var browser=get_browser();
 var browser_version=get_browser_version();

 if (browser=="MSIE"||browser=="Trident"){
    location.replace("full_demo.php"); }
    else {
    location.replace("mobile_demo.php"); }
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
user2665375
  • 3
  • 1
  • 5
  • Please format your code, thank you. – iConnor Nov 20 '13 at 20:18
  • 4
    Why do you want to redirect users of IE10 and IE11? What is the problem that you're trying to solve? – Pointy Nov 20 '13 at 20:18
  • If they are using ANY IE browser, then go to a certain page. Any other browser another page.. Thanks! – user2665375 Nov 20 '13 at 20:19
  • possible duplicate of [How can I target only Internet Explorer 11 with JavaScript](http://stackoverflow.com/questions/17447373/how-can-i-target-only-internet-explorer-11-with-javascript) – Barmar Nov 20 '13 at 20:24
  • Like @Pointy said, it'd be better to fix your JavaScript issue than it would be to sniff for browsers. Why not ask another question relating to your main issue? – jcreamer898 Nov 20 '13 at 20:24
  • 3
    I'd like to hear an answer to Pointy's question: **Why** are you trying to redirect IE users? – Ted Hopp Nov 20 '13 at 20:25
  • We have software that is only designed for IE, so IE must be used, else they get a warning page if any other browser. – user2665375 Nov 20 '13 at 20:34
  • 1
    Software "designed for IE" is probably going to have many (if not most) of the same problems in IE10/11 as it will in modern Chrome or Firefox. – Pointy Nov 20 '13 at 20:35
  • Understood, however our software only works with IE, and once they get there even using IE10/11, we have guides to enable compatibility mode. Any other browser does not work. Not sure why our developers did this, but I'm just the web guy ;) Thanks again for all your help. Very quick responses! Hopefully this helps others out there. I'll update the main post with the new code. – user2665375 Nov 20 '13 at 20:47
  • I don't think it's fair to judge WHY he wants it--just answer the question. There happen to be some pretty fugly rendering bugs that are only an issue in IE that lie outside the scope of feature detection, so I need to know how to apply them conditionally. – mpowered Jan 06 '14 at 20:32

2 Answers2

2

Your logic here (reformated):

if ((browser=="msie") && (version>=4)) {
  if (browser=="opera"||"chrome"||"safari"||"firefox") {
    location.replace("mobile_demo.php");
  }
  else {
    location.replace("full_demo.php");
  }
}

means

If the browser is "msie" and the version is 4 or greater, then if the browser is "opera", "chrome", "safari", or "firefox" go to the mobile demo, but if it's not one of those four go to the full demo.

So, you check to see if the browser is "msie", and then check to see if it's one of those other four browsers. When will it be? Never, because we only make that test when the browser is already known to be "msie". It can't be "msie" and one of the other ones at the same time, so the code always loads the full demo, unless the browser isn't "msie", in which case it does either nothing at all or something you didn't post.

edit — if all you need to do is determine whether the browser is IE or not:

if (browser == "msie")
  location.replace("full_demo.php");
else
  location.replace("mobile_demo.php");

For IE11, however, you're going to run into the problem that Microsoft has deliberately taken "MSIE" out of the useragent string. To deal with that, you could change the regex so that it matches the "Trident" string, I guess. See this MSDN resource for more details.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Got it. I used bits of code from different people. I see now what your saying, I have a nested if statement that means nothing. So I can correct that and it should work? The other code looks correct then? Thanks for your help! – user2665375 Nov 20 '13 at 20:37
  • You're welcome @user2665375 ! And note that careful code indentation is helpful because it makes spotting situations like this a lot easier. – Pointy Nov 20 '13 at 20:40
  • 1
    Modified to: if (browser=="msie"){ location.replace("mobile_demo.php"); } else { location.replace("full_demo.php"); } and seems to be working so far with IE11.. :) – user2665375 Nov 20 '13 at 20:40
  • @user2665375 - You should accept this answer. (Click on the check mark outline to the left of the answer.) – Ted Hopp Nov 20 '13 at 20:49
  • I'm actually still having a problem, now using another browser other than IE (Firefox for example) it is going to the 'full_demo.php' page.. I had them backwards, still not detecting IE – user2665375 Nov 20 '13 at 20:59
  • @user2665375 well I'm sure it's something simple - it would help if you could describe the conditions that should send somebody to the "mobile" page, and those that should go the "full" page. Should *all* desktop browsers go to the "full" page? – Pointy Nov 20 '13 at 21:20
  • If IE, then go to the 'full' page, if any other browser, then go to the 'mobile' page.. – user2665375 Nov 20 '13 at 21:33
  • I have that, and it is still not working, detecting Firefox for instance as IE for some reason.. thanks. – user2665375 Nov 20 '13 at 21:53
  • @user2665375 your "get_browser()" code correctly returns "Firefox" on Firefox. If you really have that simple test for "msie", there's no way it would load the full page. – Pointy Nov 20 '13 at 21:55
  • Your right. I made so many changes to the code I prob. screwed something up along the way. Where/how would I add in "Trident" for IE11? – user2665375 Nov 20 '13 at 22:06
  • In the regular expression; where there's that list of lower-case names separated by `|` - note that I'm not super-confident that's the right thing to do, but the IE11 useragent string is really spartan. – Pointy Nov 20 '13 at 22:11
  • Tried to add |trident in the reg expression, and ||"Trident" into the if else statement, and it doesn't detect correctly.. Not thinking I get get this working right.. – user2665375 Nov 20 '13 at 22:31
  • @user2665375 in the `if` you'd have to do `if (browser == "msie" || browser == "trident")` but I unfortunately can't check that in a real copy of IE11. – Pointy Nov 20 '13 at 22:37
  • I changed the browser=="msie" to MSIE (caps) rather and now it seems to be working.. (tested with IE9, 10, and 11). Updated original post. Thanks again for your help! – user2665375 Nov 20 '13 at 22:48
1

I have started to use feature detection to determine if it is IE11 or IE10. There is a difference in how they handle pointer events and I leverage it.

if (window.navigator.msPointerEnabled && !window.PointerEvent){

  // Using feature detection we can diff between IE 11 and IE 10.
  // Pointer events were added in IE10 (window.PointerEvent).
  // The syntax changed in IE 11 (vendor prefix was removed).
  // If pointer events are supported - and - the new syntax is supported, we know it is IE 11.
  // If pointer events supported and the new syntax is not supported -- then we know it is IE 10.

  // do something for IE10 here

} else {

   // do something for IE11 here

}
Community
  • 1
  • 1
Slopeside Creative
  • 8,901
  • 4
  • 17
  • 18
  • Feature detection works if you're using that particular feature, but if the browser changes feature implementation, then it no longer works for detecting the browser in general. – theUtherSide Jun 20 '17 at 18:36