javascript - 如何解决 1.8.2 版本的 jquery msie 错误?
问题描述
我正在开发 chrome 扩展并在其中使用 jquery v 3.2.1。该扩展在多个网站(如 20 个)上运行,并且对于 jquery 没有任何问题。但是在这样一个使用 jquery v 1.8.2 的网站上,jquery 存在一些冲突。例如,当我打开我的扩展程序时,该站点无法显示许多组件。我得到的错误是,
Uncaught TypeError: Cannot read property 'msie' of undefined
.
我从有关此主题的答案之一中尝试了以下内容。
$ = jQuery.noConflict();
jQuery.browser = {};
(function () {
jQuery.browser.msie = false;
console.log("coming inside");
jQuery.browser.version = 0;
if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) {
jQuery.browser.msie = true;
jQuery.browser.version = RegExp.$1;
}
})();
但这也不能解决我的问题。我仍然得到同样的错误。请告诉我如何解决这个错误?
解决方案
注意:这不是真正解决问题的解决方案,而是避免问题的技巧。
因此,当 jquery 3.2.1(由扩展加载)与 jquery 1.8.x(由网站加载)发生冲突时,问题就出现了。因此,我决定不为该特定网站加载 jquery。
所以,在网站的内容脚本中,我会抛出以下内容。
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
并且,在后台页面中,从我加载我的 jquery 的位置,我将添加以下内容。
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting != "hello")
{
loadjquery() // load jquery
sendResponse({farewell: "goodbye"});
}
});
推荐阅读
- python - 创建鼠标事件后保留QGraphicsItem多选移动
- github - 直接从 HPC 下载数据到 Zenodo
- imagemagick - 使用 ImageMagick 从 ttf 字体绘制字符
- python - 如何允许向客户端广播回消息给我发送消息?
- react-native - React-Native-SVG-Charts Y 轴刻度
- azure - 单区域部署的灾难恢复计划
- javascript - 无法识别 Firestore 初始更新。为什么?
- javascript - 关闭浏览器时,浏览器可以执行“关闭”WebSocket 事件吗?
- gridview - requiredfieldvalidator 在基于单选按钮列表值的网格视图中启用/禁用行编辑
- javascript - 通过嵌套数组对象属性将数组映射到字符串数组