如果..否则如果..语句在 JavaScript 中没有给出任何输出

If.... Else If.... statement is not giving any Output in javascript

本文关键字:如果 输出 任何 语句 JavaScript      更新时间:2023-09-26

如果....否则如果....在 JavaScript 中没有给出任何输出

实际上,我正在使用If.... Else If.....语句将 javascript 字符串与一些预定义navigation.userAgent字符串进行匹配,但脚本要长1.76 MB 该脚本包含一些 jquery 以及 javascript 代码 在执行时,如果条件为 true,则我没有得到任何输出If.... Else If....如语句代码块中定义的那样。请提出一些简单可行的解决方案,任何帮助都必须得到赞赏。

编辑 01

该脚本附加到我的索引.html具有<script>标签的src属性。

实际的代码块非常大,它只是该脚本的一小部分代码片段,但后面的代码与此完全相似。

$(function(){
 var winURL = $("body").attr("winredirection");
 var androidURL = $("body").attr("androidredirection");
 var mobURL = $("body").attr("mobredirection");
 var bbURL = $("body").attr("bbredirection");
 if (navigator.userAgent=="Mozilla/5.0 (compatible; U; ABrowse 0.6;Syllable) AppleWebKit/420+ (KHTML, like Gecko)") {
   window.location.assign($("body").attr("winredirection"));
 }
 else if (navigator.userAgent=="Mozilla/5.0 (compatible; ABrowse 0.4; Syllable)") {
   window.location.assign($("body").attr("winredirection"));
 }
 else if (navigator.userAgent=="Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; Acoo Browser 1.98.744; .NET CLR 3.5.30729)") {
   window.location.assign($("body").attr("winredirection"));
 }
 else if (navigator.userAgent=="Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; Acoo Browser 1.98.744; .NET CLR   3.5.30729)"){window.location.assign($("body").attr("winredirection"));}else if (navigator.userAgent=="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0;   Acoo Browser; GTB5; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;   SV1) ; InfoPath.1; .NET CLR 3.5.30729; .NET CLR 3.0.30618)"){window.location.assign($("body").attr("winredirection"));}else if (navigator.userAgent=="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; Acoo Browser; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; Avant Browser)"){window.location.assign($("body").attr("winredirection"));}
});

编辑 02

我的索引.html页

<!DOCTYPE html> 
<html> 
<head> 
<title> 
  Platform Identification using perfect.api.js
</title> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 
<script src="perfect.api.js"></script> 
</head> 
<body winredirection="http://google.com"  androidredirection="www.google.com/mobile/android" mobredirection="www.google.com/mobile" bbredirection="www.google.com/mobile/bb"> 
</body> 
</html>

谢谢

接口开发者

如果您打算检测移动设备并因此进行重定向,则应检查用户代理字符串中的特定令牌,而不是完全匹配,因为完全匹配不太可能匹配,因为它们会因操作系统版本、浏览器版本以及在某些情况下设备上安装的软件而异。

要检查移动设备,请尝试检测移动浏览器中的答案

首先,

我绝对建议不要手动解析用户代理字符串,因为代码很容易变得一团糟,尤其是您使用一大堆 if/else if 语句执行此操作的方式。我会使用这样的库:https://github.com/faisalman/ua-parser-js。使用它的 getOs() 函数来确定正在使用的操作系统,或者使用它的其他函数来确定正在使用的设备,而不是匹配特定的用户代理。

您当前拥有的代码看起来不错,我无法对其进行测试,因为我在示例中提供的用户代理字符串中没有任何浏览器,但我建议使用上面链接的库执行类似操作:

var parser = new UAParser();
var os = parser.getOS();
if(os.name == "windows") {
 $("body").attr("winredirection");
}
//other operating systems and/or devices go below.