javascript 如果 IE8 或更低版本,则强制更新浏览器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10044855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Force browser update if IE8 or older
提问by ProDraz
I wonder if it's possible to show a warning or open a pop-up, which would say update your IE to latest version or use Firefox / Chrome / Safari instead, when browser is Internet Explorer IE8 or older...
我想知道是否可以显示警告或打开一个弹出窗口,当浏览器是 Internet Explorer IE8 或更旧的版本时,它会说将您的 IE 更新到最新版本或使用 Firefox / Chrome / Safari 代替...
I guess I should use that code below inside the tags...
我想我应该在标签内使用下面的代码......
<!--[if lt IE 9]>
...i should use code here...
<![endif]-->
Is it smart to deceted browser with jQuery and loading jQuery lib? Or is it better to just use regular javascript in order to avoid additional issues with very old browsers?
用 jQuery 欺骗浏览器并加载 jQuery 库是否聪明?或者,为了避免非常旧的浏览器出现其他问题,最好只使用常规 javascript 吗?
采纳答案by antonjs
You have two options:
您有两个选择:
Parse the the
User-Agent String
// Returns the version of Internet Explorer or a -1 // (indicating the use of another browser). function getInternetExplorerVersion() { 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. ); } } return rv; } function checkVersion() { var msg = "You're not using Internet Explorer."; var ver = getInternetExplorerVersion(); if ( ver > -1 ) { if ( ver >= 9.0 ) { msg = "You're using a recent copy of Internet Explorer." } else { msg = "You should upgrade your copy of Internet Explorer."; } } alert(msg); }
Using Conditional Comments:
<!--[if gte IE 9]> <p>You're using a recent version of Internet Explorer.</p> <![endif]--> <!--[if lt IE 8]> <p>Hm. You should upgrade your copy of Internet Explorer.</p> <![endif]--> <![if !IE]> <p>You're not using Internet Explorer.</p> <![endif]>
解析
User-Agent String
// Returns the version of Internet Explorer or a -1 // (indicating the use of another browser). function getInternetExplorerVersion() { 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. ); } } return rv; } function checkVersion() { var msg = "You're not using Internet Explorer."; var ver = getInternetExplorerVersion(); if ( ver > -1 ) { if ( ver >= 9.0 ) { msg = "You're using a recent copy of Internet Explorer." } else { msg = "You should upgrade your copy of Internet Explorer."; } } alert(msg); }
使用条件注释:
<!--[if gte IE 9]> <p>You're using a recent version of Internet Explorer.</p> <![endif]--> <!--[if lt IE 8]> <p>Hm. You should upgrade your copy of Internet Explorer.</p> <![endif]--> <![if !IE]> <p>You're not using Internet Explorer.</p> <![endif]>
Reference: Detecting Windows Internet Explorer More Effectively
回答by Claudio Redi
You could use something like this
你可以使用像这样
The ie6-upgrade-warning is a little script (7.9kb) that displays a warning message politely informing the user to upgrade the browser to a newer version (links to newest IE, Firefox, Opera, Safari, Chrome are provided).
The webpage is still visible behind a transparent background, but access to it is prevented. The idea is to force users to upgrade from IE6 and avoid the website from a bad reputation that website is not rendering correctly in IE6.
The script is completely translatable in any language, very easy to set-up (one line of code in webpage and one parametter configuration).
ie6-upgrade-warning 是一个小脚本 (7.9kb),它显示一条警告消息,礼貌地通知用户将浏览器升级到更新版本(提供了指向最新 IE、Firefox、Opera、Safari、Chrome 的链接)。
该网页在透明背景下仍然可见,但无法访问。这个想法是强制用户从 IE6 升级,并避免网站在 IE6 中无法正确呈现的不良声誉。
该脚本完全可以用任何语言翻译,非常容易设置(网页中的一行代码和一个参数配置)。
Although was created to be used with IE6 users, using the correct parameters you can use it for your scenario.
虽然创建用于 IE6 用户,但使用正确的参数您可以将其用于您的场景。
回答by David Zorychta
<script> var ISOLDIE = false; </script>
<!--[if lt IE 9]>
<script> var ISOLDIE = true; </script>
<![endif]-->
Then later on, where ever you want to draw the line for supporting older versions of IE in your code:
稍后,您想在代码中为支持旧版本的 IE 划清界限:
<script>
if(ISOLDIE) {
alert("Your browser currently does not support this feature. Please upgrade.");
window.location = 'http://google.com/chrome';
}
</script>
It's typically not a good idea to completely remove IE8 support, which is why I think the above solution is a good compromise, because you can add it to components of your website/web application which simply cannot be made to support IE8, but then you could (possibly) still support IE8 in other areas of your website.
完全删除 IE8 支持通常不是一个好主意,这就是为什么我认为上述解决方案是一个很好的折衷方案,因为您可以将其添加到您的网站/Web 应用程序的组件中,而这些组件根本无法支持 IE8,但随后您可以(可能)仍然在您网站的其他区域支持 IE8。
回答by Erik Terwan
There is this dutch website forcing IE6 and lower users to upgrade their browser, a little adjustment to the "If IE" code you can block from whatever version you like.
有这个荷兰网站迫使 IE6 和更低版本的用户升级他们的浏览器,对“If IE”代码稍作调整,您可以阻止您喜欢的任何版本。
Scroll down to step 4 and click download or "voorbeeld" for a demo, the first one is a top banner, the second one completely blocks the page.
向下滚动到第 4 步,然后单击下载或“voorbeeld”进行演示,第一个是顶部横幅,第二个是完全屏蔽页面。
The only downside, its all in dutch so you'd have to make up your own message.
唯一的缺点是全部都是荷兰语,因此您必须自己编写消息。
回答by thE_iNviNciblE
回答by Ankit Suthar
On your site add your code to index.html or index.php
在您的网站上将您的代码添加到 index.html 或 index.php
Its working also joomla and wordpress.
它的工作也joomla 和wordpress。
<!--[if IE 5]>
<script language="Javascript">
<!--
alert ("It looks like you aren't using Internet Explorer 7. To see our site correctly, please update.")
//-->
</script>
<![endif]-->
<!--[if IE 5.0]>
!--
alert ("It looks like you aren't using Internet Explorer 7. To see our site correctly, please update.")
//-->
</script>
<![endif]-->
<!--[if IE 5.5]>
!--
alert ("It looks like you aren't using Internet Explorer 7. To see our site correctly, please update.")
//-->
</script>
<![endif]-->
<!--[if IE 6]>
!--
alert ("It looks like you aren't using Internet Explorer 7. To see our site correctly, please update.")
//-->
</script>
<![endif]-->
<!--[if IE 7]>
!--
alert ("It looks like you aren't using Internet Explorer 7. To see our site correctly, please update.")
//-->
</script>
<![endif]-->
<!--[if IE 8]>
!--
alert ("It looks like you aren't using Internet Explorer 8. To see our site correctly, please update.")
//-->
</script>
<![endif]-->
回答by drjorgepolanco
There are many ways, but this is by far the most elegant: http://outdatedbrowser.com/en/how
有很多方法,但这是迄今为止最优雅的:http: //outdatedbrowser.com/en/how