检测和重定向

时间:2014-06-24 14:14:08

标签: javascript redirect

我使用JavaScript来检测Java和Symbian以及Android移动设备并检测PD

<script>

var isAndroidMobile = {
    Android: function() {
        return navigator.userAgent.match(/android/i);
    }
};

var isSymbianMobile = {
    Symbian: function() {
        return navigator.userAgent.match(/series60|symbian/i);
    }
};

var isJavaMobile = {
    Java: function() {
        return navigator.userAgent.match(/java|mdp|J2ME|midp|UC/i);
    }
};

var isPc = {
    Pc: function() {
        return navigator.userAgent.match(/windows|linux|centos|ubuntu/i);
    }
};

if( isJavaMobile.Java() ) alert('You are useing java mobile!');
    {{ window.location = 'http://domain/prog.jar' }}
if( isSymbianMobile.Symbian() ) alert('You are useing symbian mobile!');
    {{ window.location = 'http://domain/prog.sisx' }}
if( isPc.Pc() ) alert('You are useing Pc!');
    {{ window.location = 'http://domain/prog.exe' }}
if( isAndroidMobile.Android() ) alert('You are useing android mobile!');
    {{ window.location = 'http://domain/prog.apk' }}

</script>

但是当它检测到PC或Java或Symbian时,它会将其重定向到Android

如果我设置

if( isSymbianMobile.Symbian() ) alert('You are useing symbian mobile!');
    {{ window.location = 'http://domain/prog.sisx' }}
if( isPc.Pc() ) alert('You are useing Pc!');
    {{ window.location = 'http://domain/prog.exe' }}
if( isAndroidMobile.Android() ) alert('You are useing android mobile!');
    {{ window.location = 'http://domain/prog.apk' }}
if( isJavaMobile.Java() ) alert('You are useing java mobile!');
    {{ window.location = 'http://domain/prog.jar' }}

将它重定向到Java

当我把这个Java代码放到最后时,它将重定向到Java 当我把这个Symbian代码放到最后时,它将重定向到Symbian等等。

我能解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

看起来好像你的JavaScript被破坏了。尝试更改它,以便alertwindow.locationif声明中。

if( isJavaMobile.Java() ) {
    alert('You are useing java mobile!');
    window.location = 'http://domain/prog.jar' ;
}
if( isSymbianMobile.Symbian() ) {
    alert('You are useing symbian mobile!');
    window.location = 'http://domain/prog.sisx';
}
if( isPc.Pc() ) {
    alert('You are useing Pc!');
    window.location = 'http://domain/prog.exe';
}
if( isAndroidMobile.Android() ) {
    alert('You are useing android mobile!');
    window.location = 'http://domain/prog.apk';
}