javascript – ReactJS中的浏览器检测
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – ReactJS中的浏览器检测,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1750字,纯文字阅读大概需要3分钟。
内容图文
![javascript – ReactJS中的浏览器检测](/upload/InfoBanner/zyjiaocheng/701/88fb89c66ed34fcfadf91f6a547f2053.jpg)
有没有办法检测IE浏览器与React并重定向到页面或提供任何有用的消息.我在JavaScript中找到了一些东西,但不确定如何将它与React TypeScript一起使用.
var isEdge =!isIE&& !! window.StyleMedia;
解决方法:
您可以使用这些来有条件地渲染jsx或帮助路由…
我使用了以下内容并取得了巨大成功.
最初来自 – How to detect Safari, Chrome, IE, Firefox and Opera browser?
// Opera 8.0+
const isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
// Firefox 1.0+
const isFirefox = typeof InstallTrigger !== 'undefined';
// Safari 3.0+ "[object HTMLElementConstructor]"
const isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || (typeof safari !== 'undefined' && safari.pushNotification));
// Internet Explorer 6-11
const isIE = /*@cc_on!@*/false || !!document.documentMode;
// Edge 20+
const isEdge = !isIE && !!window.StyleMedia;
// Chrome 1 - 71
const isChrome = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime);
// Blink engine detection
const isBlink = (isChrome || isOpera) && !!window.CSS;
请注意,由于浏览器更改,他们每个人都有机会弃用.
我在React中使用它们是这样的:
content(props){
if(!isChrome){
return (
<Otherjsxelements/>
)
}
else {
return (
<Chromejsxelements/>
)
}
}
然后通过调用主组件中的{this.Content()}来呈现不同的浏览器特定元素.
伪代码可能看起来像这样……(未经测试):
import React from 'react';
const isChrome = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime);
export default class Test extends React.Component {
content(){
if(isChrome){
return (
<div>Chrome</div>
)
} else {
return (
<div>Not Chrome</div>
)
}
}
render() {
return (
<div>Content to be seen on all browsers</div>
{this.content()}
)
}
}
内容总结
以上是互联网集市为您收集整理的javascript – ReactJS中的浏览器检测全部内容,希望文章能够帮你解决javascript – ReactJS中的浏览器检测所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。