如果..否则如果..语句在 JavaScript 中没有给出任何输出
If.... Else If.... statement is not giving any Output in javascript
如果....否则如果....在 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.
- 如果 a 为 false,则 if(a) === if(false)
- RegEx删除空属性?例如,如果(class=“”||class=“”)移除;否则就下课
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- 如果 CreateJS 库支持 Texture Packer 的 JSON 输出格式
- 如果是,则切换具有不同输出的情况
- 如果 java 脚本中的其他条件,则无法正确输出
- 如果是周末,则输出 x
- 如果我更改输入顺序,为什么控制台会给我不同的输出,如下所示
- 如果用户在收到提示后将任何信息留空,我如何从 Javascript 的输出 (window.alert) 中排除该数据
- 如果功能多次激活,请多次停止输出打印
- 引导 3 工具提示 - 轻拂鼠标输出..如果它在 iFrame 中发生
- 基本的JavaScript,如果给出意外的输出,则有多个其他
- Javascript eval 在计算数学表达式时,如果使用的数字以 0 开头,则给出错误的输出
- Javascript检查函数的结果是否已经输出,如果是,则运行该函数
- javascript检查函数并给出输出(如果存在)
- 检查是否有cookie设置,如果没有输出脚本
- 如果我不知道我将返回什么,我如何整齐地输出json对象中的所有数据?
- “如果逻辑”基于胡子中的函数输出.js
- 如果..否则如果..语句在 JavaScript 中没有给出任何输出
- 如果变量包含,则输出包含的变量