2015-06-14 39 views
-2

如果....否則如果....是不是給在JavaScript如果....否則如果....聲明沒有給出任何輸出在javascript

Acctually我匹配任何輸出navigation.userAgent JavaScript的字符串與一些預定義的字符串使用If.... Else If.....聲明,但腳本是長約1.76 MB該腳本包含一些jquery以及JavaScript代碼在執行時,我沒有得到任何輸出,如果條件爲真如If.... Else If....聲明中定義代碼塊。請建議一些簡單和工作的解決方案,必須讚賞任何幫助。

編輯01

腳本連接到我的index.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

我的index.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> 

感謝,

API開發

+0

答案使用switch語句或調度陣列 –

+0

@Ed_Heal您可以提供的代碼 –

+0

以及爲什麼問題導致? –

回答

0

首先我肯定會推薦給不是M因爲代碼很容易變成一團亂麻,特別是你用一大堆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. 
+0

我不想使用任何第三方庫 –

+0

以及你可以自己手動編寫代碼,有大量的資源在線使用正則表達式來檢測哪個設備正在使用。你爲什麼不想使用第三方庫? –

+0

我在使用其他人創建的jQuery時使用其他人創建的項目 –

1

如果你的目的是爲了檢測移動設備和重定向結果,你應該檢查在用戶代理字符串,而不是完全匹配的特定標記,精確匹配是不可能匹配,因爲它們會根據操作系統版本,瀏覽器版本以及某些情況下安裝在設備上的軟件而有所不同。

要檢查移動設備,嘗試從Detecting a mobile browser

+0

尼斯點感謝我會記住,並將解決當前的錯誤後,我的腳本一定會實現。 –