无法在 IE 上的 window 中存储对象
Not able to store an object in window on IE
我想在 window
中存储一个对象。目前它正在 Chrome、Edge、Firefox 但不是 IE。
我的代码:
(function() {
'use strict';
var bottom = Object.create(null);
bottom = {};
bottom.tools = {};
bottom.tools.detectIEEdge = detectIEEdge;
function detectIEEdge() {
var ua = window.navigator.userAgent,
msie, trident, rv;
msie = ua.indexOf('MSIE ');
if (msie > 0) {
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}
trident = ua.indexOf('Trident/');
if (trident > 0) {
rv = ua.indexOf('rv:');
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}
return false;
}
window.bottom = bottom;
})();
我的代码在 HTML 中:
<script>
console.log(window.bottom) // undefined
</script>
你说,“我的主要目标是提供一个检测用户是否使用 IE 的功能。”
建议您参考下面的示例,可以帮助您识别 IE 浏览器及其版本。
<!DOCTYPE html>
<html>
<head>
<title>
Detects user uses Internet Explorer
</title>
</head>
<body>
<center>
<h1 style="color:green">Test Demo</h1>
<script>
//detects if user uses Internet Explorer
//returns version of IE or false, if browser is not IE
//Function to detect IE or not
function IEdetection() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf('MSIE ');
if (msie > 0) {
// IE 10 or older, return version number
return ('IE ' + 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 ('IE ' + parseInt(ua.substring(
rv + 3, ua.indexOf('.', rv)), 10));
}
// User uses other browser
return ('Not IE');
}
var result = IEdetection();
document.write(result);
</script>
</center>
</body>
</html>
输出:
参考:
How to check the user is using Internet Explorer in JavaScript?
我想在 window
中存储一个对象。目前它正在 Chrome、Edge、Firefox 但不是 IE。
我的代码:
(function() {
'use strict';
var bottom = Object.create(null);
bottom = {};
bottom.tools = {};
bottom.tools.detectIEEdge = detectIEEdge;
function detectIEEdge() {
var ua = window.navigator.userAgent,
msie, trident, rv;
msie = ua.indexOf('MSIE ');
if (msie > 0) {
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}
trident = ua.indexOf('Trident/');
if (trident > 0) {
rv = ua.indexOf('rv:');
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}
return false;
}
window.bottom = bottom;
})();
我的代码在 HTML 中:
<script>
console.log(window.bottom) // undefined
</script>
你说,“我的主要目标是提供一个检测用户是否使用 IE 的功能。”
建议您参考下面的示例,可以帮助您识别 IE 浏览器及其版本。
<!DOCTYPE html>
<html>
<head>
<title>
Detects user uses Internet Explorer
</title>
</head>
<body>
<center>
<h1 style="color:green">Test Demo</h1>
<script>
//detects if user uses Internet Explorer
//returns version of IE or false, if browser is not IE
//Function to detect IE or not
function IEdetection() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf('MSIE ');
if (msie > 0) {
// IE 10 or older, return version number
return ('IE ' + 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 ('IE ' + parseInt(ua.substring(
rv + 3, ua.indexOf('.', rv)), 10));
}
// User uses other browser
return ('Not IE');
}
var result = IEdetection();
document.write(result);
</script>
</center>
</body>
</html>
输出:
参考:
How to check the user is using Internet Explorer in JavaScript?