0

我有这个针对 JS 的移动 userAgent 测试,但不太确定如何正确嵌套它以扩展测试。

基本上现在它测试是移动设备还是桌面设备,我希望它进一步做的是移动设备,检查哪个移动设备并做某事。

<script type="text/javascript">
  if( /Android|iPhone|iPod|iPad|BlackBerry|Windows Phone/i.test(navigator.userAgent) ) {
    if ( /Android/i.test(navigator.userAgent) ) {
        var url = window.location.href = 'http://www.google.com';
        url.show(); 
    }
    elseif 
    if ( /iPhone/i.test(navigator.userAgent) ) {
        var url = window.location.href = 'http://www.bing.com';
        url.show(); 
    }
  }
    else
  {

  }
</script>

我知道上面的内容是错误的,但据我所知,它没有解决。

4

2 回答 2

0

而不是重复自己,你可以做这样的事情:

if( /big regex here/i.test(navigator.userAgent)) {
    var agents = {
        "Android":"http://google.com/",
        "iPhone":"http://bing.com/"
    }, i, url;
    for( i in agents) {
        if( new Regexp(i,"i").test(navigator.userAgent)) {
            url = window.location.href = agents[i];
            url.show();
            break;
        }
    }
}
于 2013-01-07T18:00:34.610 回答
0

这里

<script type="text/javascript">
  var url="notmobile.html";
  if( /Android|iPhone|iPod|iPad|BlackBerry|Windows Phone/i.test(navigator.userAgent) ) {
    if ( /Android/i.test(navigator.userAgent) ) {
        url = 'http://www.google.com';
    }
    else if ( /iPhone/i.test(navigator.userAgent) ) {
        url = 'http://www.bing.com';
    }
//   url.show(); // some special mobile meaning? - never seen that syntax
    location=url; // this is how I would do it
  }
  else { // not mobile
    // this can be moved out side the ifs with the above location change too
    location = url;
  }
</script>
于 2013-01-07T18:00:37.020 回答