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"); }
有帮助吗?

解决方案

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.

其他提示

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

}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top