Isl*_*aev 8 internet-explorer google-chrome cross-browser reactjs
我正在研究一个反应应用程序,如果用户的旧浏览器不支持(例如IE 9),客户希望它显示特殊消息.
很长一段时间我试图使用react-device-detect包来检测一些"流行的"旧浏览器.
SRC/index.js
import { browserName, browserVersion } from "react-device-detect";
const render = Component => {
if (browserName === "IE" && browserVersion < 10) {
ReactDOM.render(<UnsupportedBrowser />, document.getElementById("root"));
} else {
ReactDOM.render(
<AppContainer>
<Component store={store} history={history} />
</AppContainer>,
document.getElementById("root")
);
}
};
Run Code Online (Sandbox Code Playgroud)
并提出条件评论:
公共/ index.html的
<!--[if lte IE 9]>
Please upgrade your browser
<![endif]-->
Run Code Online (Sandbox Code Playgroud)
但我怀疑,有更好的方法,我找不到搜索网络.
小智 14
我找到了很多用于检测用户浏览器名称和版本的JS脚本,因此我不得不将它们混合以获得我想要的内容
<script type="text/javascript">
function get_browser() {
var ua = navigator.userAgent, tem, M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
if (/trident/i.test(M[1])) {
tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
return { name: 'IE', version: (tem[1] || '') };
}
if (M[1] === 'Chrome') {
tem = ua.match(/\bOPR\/(\d+)/)
if (tem != null) { return { name: 'Opera', version: tem[1] }; }
}
if (window.navigator.userAgent.indexOf("Edge") > -1) {
tem = ua.match(/\Edge\/(\d+)/)
if (tem != null) { return { name: 'Edge', version: tem[1] }; }
}
M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'];
if ((tem = ua.match(/version\/(\d+)/i)) != null) { M.splice(1, 1, tem[1]); }
return {
name: M[0],
version: +M[1]
};
}
var browser = get_browser()
var isSupported = isSupported(browser);
function isSupported(browser) {
var supported = false;
if (browser.name === "Chrome" && browser.version >= 48) {
supported = true;
} else if ((browser.name === "MSIE" || browser.name === "IE") && browser.version >= 10) {
supported = true;
} else if (browser.name === "Edge") {
supported = true;
}
return supported;
}
if (!isSupported) {
document.write(<h1>My message</h1>)
}
</script>
Run Code Online (Sandbox Code Playgroud)
如果用户的浏览器是chrome> = 48或者> = 10或任何版本的边缘,则此脚本允许用户继续.否则,它会显示一条特殊消息,要求用户更新或更改其浏览器.
您还可以根据需要自定义此脚本,修改isSupported()函数.
我认为最好和最用户友好的替代方法是将用户重定向到一个专有ie.html页面,在那里您可以显示有关如何下载其他浏览器的说明,并且只关心该页面中的所有 IE 内容。
这样你就不需要对 React 做任何事情,只需将以下几行添加到你的index.html:
<script type="application/javascript">
if (/MSIE|Trident/.test(window.navigator.userAgent)) window.location.href = '/ie.html';
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4167 次 |
| 最近记录: |