首页 > 解决方案 > 如何解决 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;
    }
})();

但这也不能解决我的问题。我仍然得到同样的错误。请告诉我如何解决这个错误?

标签: javascriptjquerygoogle-chrome-extension

解决方案


注意:这不是真正解决问题的解决方案,而是避免问题的技巧。

因此,当 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"});
}
});

推荐阅读