Wykrywanie Internet Explorer 11
Wiem, że IE 11 ma inny ciąg agenta użytkownika niż wszystkie inne IE
Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 11.0) like Gecko
Próbowałem wykryć IE 11 z odpowiedzią podaną na to pytanie "
Thats !!navigator.userAgent.match(/Trident\/7\./)
Ale dostaję błąd
Object not found and needs to be re-evaluated.
Następnie otwieram konsolę dewelopera w IE11 i próbowałem uzyskać dostęp do niektórych predefiniowanych obiektów javascript, wciąż dostaję ten sam błąd.
Mam tried
navigator.userAgent
window.navigator
console.log('test');
8 answers
Edycja 18 Lis 2016
Ten kod działa również ( dla tych, którzy preferują inne rozwiązanie, bez użycia ActiveX )
var isIE11 = !!window.MSInputMethodContext && !!document.documentMode;
// true on IE11
// false on Edge and other IEs/browsers.
Oryginalna ODPOWIEDŹ
Aby sprawdzić Ie11 , możesz użyć tego : ( tested)
(lub run this )
!(window.ActiveXObject) && "ActiveXObject" in window
Mam wszystkie VMS IE:
Notice: this wont work for IE12:
Jak widać tutaj, to zwraca true:
Więc co możemy zrobić:
Najwyraźniej dodali przestrzeń bitową maszyny:
Ie11:
"Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; rv:11.0) like Gecko"
Ie12:
"Mozilla/5.0 (Windows NT 6.3; Win64; x64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; rv:11.0) like Gecko"
Więc możemy zrobić:
/x64|x32/ig.test(window.navigator.userAgent)
Zwróci true tylko dla ie12.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-19 11:32:51
Aby szybko wykryć MSIE (od wersji 6 do 11):
if(navigator.userAgent.indexOf('MSIE')!==-1
|| navigator.appVersion.indexOf('Trident/') > -1){
/* Microsoft Internet Explorer detected in. */
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-18 14:35:50
Wszystkie powyższe odpowiedzi ignorują fakt, że wspominasz, że nie masz okna ani nawigatora: -)
Potem otwieram konsolę dewelopera w IE11
I tam jest napisane
Obiekt nie został znaleziony i musi zostać ponownie oceniony.
I navigator, window, console, żaden z nich nie istnieje i wymaga ponownej oceny. Miałem to w emulacji. wystarczy kilka razy zamknąć i otworzyć konsolę.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-18 09:05:06
Używam następującej funkcji do wykrywania wersji 9, 10 i 11 IE:
function ieVersion() {
var ua = window.navigator.userAgent;
if (ua.indexOf("Trident/7.0") > -1)
return 11;
else if (ua.indexOf("Trident/6.0") > -1)
return 10;
else if (ua.indexOf("Trident/5.0") > -1)
return 9;
else
return 0; // not IE9, 10 or 11
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-18 14:36:24
I jak to zaimplementowałem
<script type="text/javascript">
!(window.ActiveXObject) && "ActiveXObject"
function isIE11(){
return !!navigator.userAgent.match(/Trident.*rv[ :]*11\./);
}
</script>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-08 12:24:46
Ten link był pomocny . Zawiera kod javascript do wykrywania wszystkich wersji IE do IE11. Testowałem skrypt z emulatorem IE11. Aby znaleźć emulator IE11, kliknij prawym przyciskiem myszy na przeglądarce internetowej kliknij "Inspect element". W lewym dolnym rogu strony przewiń w dół pasek nawigacji i kliknij ikonę pulpitu. Pole rozwijane "User Agent String" zawiera opcje do emulowania IE6-11.
To działa. Użyłem go kilka minut przed napisaniem tej odpowiedzi. Cannot post migawki - za mało reputacji.To jest kod-kliknij w link, aby zobaczyć go ponownie:
// Get IE or Edge browser version
var version = detectIE();
if (version === false) {
document.getElementById('result').innerHTML = '<s>IE/Edge</s>';
} else if (version >= 12) {
document.getElementById('result').innerHTML = 'Edge ' + version;
} else {
document.getElementById('result').innerHTML = 'IE ' + version;
}
// add details to debug result
document.getElementById('details').innerHTML = window.navigator.userAgent;
/**
* detect IE
* returns version of IE or false, if browser is not Internet Explorer
*/
function detectIE() {
var ua = window.navigator.userAgent;
// Test values; Uncomment to check result …
// IE 10
// ua = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)';
// IE 11
// ua = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko';
// Edge 12 (Spartan)
// ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0';
// Edge 13
// ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586';
var msie = ua.indexOf('MSIE ');
if (msie > 0) {
// IE 10 or older => return version number
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}
var trident = ua.indexOf('Trident/');
if (trident > 0) {
// IE 11 => return version number
var rv = ua.indexOf('rv:');
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}
var edge = ua.indexOf('Edge/');
if (edge > 0) {
// Edge (IE 12+) => return version number
return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
}
// other browser
return false;
}
@import url(https://fonts.googleapis.com/css?family=Fira+Mono|Fira+Sans:300);
body {
color: black;
background-color: white;
font-family: "Fira Sans", sans-serif;
font-weight: 300;
margin: 0;
padding: 3rem;
}
h1 {
color: darkgrey;
text-align: center;
font-weight: 300;
font-size: 1.5rem;
line-height: 2rem;
}
h2 {
text-align: center;
font-weight: 300;
font-size: 4rem;
}
p {
color: darkgrey;
text-align: center;
font-family: "Fira Mono", monospace;
font-size: 1rem;
line-height: 1.5rem;
}
<h1>Detect IE/Edge version with JavaScript.<br> Updated to recognize Internet Explorer 12+ aka Edge.</h1>
<h2 id="result">detecting…</h2>
<p id="details">n/a</p>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-05 15:12:23
Używanie tego wyrażenia regularnego wydaje się działać dla IE 10 i IE 11:
function isIE(){
return /Trident\/|MSIE/.test(window.navigator.userAgent);
}
Nie mam IE starszego niż IE 10, aby to przetestować.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-15 15:28:30
Użyj Nawigatora:-
{[2] } jest obiektem, który zawiera wszystkie informacje o przeglądarce komputera klienta.
navigator.appName
zwraca nazwę przeglądarki komputera klienta.
navigator.appName === 'Microsoft Internet Explorer' || !!(navigator.userAgent.match(/Trident/) || navigator.userAgent.match(/rv:11/)) || (typeof $.browser !== "undefined" && $.browser.msie === 1) ? alert("Please dont use IE.") : alert("This is not IE")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-10 05:10:21