2013-07-02 43 views
1

我想根據用戶是否運行IE來運行JQuery函數。原因是因爲我擁有的功能在IE中不起作用,所以當用戶使用IE時,我想另外運行另一個功能。我正在運行JQuery 1.9.1,所以我不能使用$.browser。我試圖使用有條件的評論,但我不確定這是否應該走。我試圖在這裏實現它,但它不工作:http://jsfiddle.net/AsQ4E/當瀏覽器是IE時運行JQuery函數

這是HTML

<!--[if IE]> 
    <input type="button" onclick="javascript:ie();" value="Click Me!" /> 
<![endif]--> 

<!--[if !IE]> --> 
    <input type="button" onclick="javascript:notIE();" value="Click Me!" /> 
<!-- <![endif]--> 

這是JQuery的

function ie() { 
    alert("You are using IE"); 
} 

function notIE() { 
    alert("You are not using IE"); 
} 

我如何能得到這個工作?這是最好的辦法嗎?


編輯:我想使用SVG Crowbar。當用戶點擊一個按鈕時,Crowbar會從頁面中提取一個SVG並彈出一個下載。這裏是它的鏈接:http://nytimes.github.io/svg-crowbar/。問題是,它不能在IE中工作,所以我打算只在用戶使用IE時使用它:http://bl.ocks.org/biovisualize/1209499(這在IE中有效)。我不太瞭解編程和Javascript來調試IE的問題。

+0

你的代碼工作正常。你只需要檢查小提琴上的設置,默認值是不正確的。你的功能需要在''中才能使按鈕工作(將'onLoad'改爲'不換行 - 在'中)。另外如果您使用的是jQuery,請嘗試將事件*正確綁定*。意思是,不要使用'onclick'屬性。 –

+2

向我們展示您在JavaScript中無法使用的JavaScript功能。此外,條件註釋在IE10中不起作用。 – lifetimes

+1

什麼是在IE中不起作用的功能?爲什麼它不起作用?什麼對它不起作用?通常檢查功能比檢查瀏覽器更好。所以,這裏可能有比檢查IE更好的解決方案! –

回答

0

你有沒有試過看「navigator.userAgent」。你可以很清楚地知道它的IE。還有其他有用的瀏覽器檢測腳本,但如果你只是需要一個簡單的IE檢查,我會堅持以上。

1

用JavaScript使用導航儀現在

navigator.userAgent.indexOf('MSIE') 

如果你真的想要的IE瀏覽器,你應該從MSDN檢查此腳本版本

Detecting Windows Internet Explorer More Effectively

+1

感謝上帝,它不會在IE 11上工作http://www.engadget.com/2013/03/25/ie-11-says-it-is-like-firefox/ – Pavlo

+1

關於msdn鏈接的很好的信息Jorge注意使用indexOf與IE9以下的IE9 – jjay225

0

下面是使用propopsed函數的例子通過MSDN

http://msdn.microsoft.com/en-us/library/ms537509(v=vs.85).aspx

<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta charset="utf-8" /> 
    <title></title> 
    <script type="text/javascript"> 
     function getInternetExplorerVersion() 
      // Returns the version of Internet Explorer or a -1 
      // (indicating the use of another browser). 
     { 
      var rv = -1; // Return value assumes failure. 
      if (navigator.appName == 'Microsoft Internet Explorer') { 
       var ua = navigator.userAgent; 
       var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})"); 
       if (re.exec(ua) != null) 
        rv = parseFloat(RegExp.$1); 
      } 
      return rv; 
     } 

     function myFunction() { 
      if (getInternetExplorerVersion() > -1) 
       ie(); 
      else 
       notIE(); 
     } 

     function ie() { 
      alert("You are using IE"); 
     } 

     function notIE() { 
      alert("You are not using IE"); 
     } 
    </script> 
</head> 
<body> 
    <input type="button" onclick="javascript:myFunction();" value="Click Me!" /> 
</body> 
</html> 
+0

IE10返回-1。我將如何解決這個問題? – TFischer

0

您可以使用下面的函數來檢查

(function() { 
    "use strict"; 
    var tmp = (document["documentMode"] || document.attachEvent) && "ev" 
     , msie = tmp 
        && (tmp = window[tmp + "al"]) 
        && tmp("/*@cc_on 1;@*/") 
        && +((/msie (\d+)/i.exec(navigator.userAgent) || [])[1] || 0) 
    ; 
    return msie || void 0;})(); 

但是療法是您可以使用更多的選擇。看看http://www.jquery4u.com/browsers-2/check-ie-version/

0

另一種方法是使用條件註釋,阿拉HTML5 Boilerpalte

HTML

<!DOCTYPE html> 
<!--[if lt IE 7]>  <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--> 
<!--[if IE 7]>   <html class="no-js lt-ie9 lt-ie8"> <![endif]--> 
<!--[if IE 8]>   <html class="no-js lt-ie9"> <![endif]--> 
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]--> 

JS

// tweak depending on version of IE 
var isIe = $('.lt-ie9').length > 0; // or tagName/document.body etc... 

button.on('click', function() { 
    if (isIe) { 
    console.log('ie'); 
    } 
    else { 
    console.log('not ie'); 
    } 
}); 

當你modernizr結合本,你會得到「好處」 「UA嗅探全功能檢測。在JavaScript

0

條件註釋,而不是訴諸用戶代理嗅探:

var ie = (function(){ 

    var undef, 
     v = 3, 
     div = document.createElement('div'), 
     all = div.getElementsByTagName('i'); 

    while (
     div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->', 
     all[0] 
    ); 

    return v > 4 ? v : undef; 

}()); 

if(ie) 
    alert('You are using IE version' + ie); 
else 
    alert('You are not using IE'); 
+0

這是否適用於IE10?或者它是我啓用的設置http://jsfiddle.net/AsQ4E/3/ – TFischer

相關問題