我在Chrome开发控制台得到以下错误:
Uncaught TypeError: Cannot read property 'msie' of undefined
我的理解是,这是因为.browser现在在jQuery中已弃用,但我使用的是最新版本的jQuery工具,它仍然会给出错误,我检查了js文件,它就在那里。
我怎样才能避免这个错误呢?
我在Chrome开发控制台得到以下错误:
Uncaught TypeError: Cannot read property 'msie' of undefined
我的理解是,这是因为.browser现在在jQuery中已弃用,但我使用的是最新版本的jQuery工具,它仍然会给出错误,我检查了js文件,它就在那里。
我怎样才能避免这个错误呢?
当前回答
这个问题主要是由浏览器导航和版本属性引起的。 为此,你必须在函数中添加以下代码:
/* solution to undefined msie */
jQuery.browser = {};
jQuery.browser.msie = false;
jQuery.browser.version = 0;
if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) {
jQuery.browser.msie = true;
jQuery.browser.version = RegExp.$1;
}
/* solution to undefined msie */
例如,我正在使用它的点击功能,看到它:
$(document).on('click', '.remove_add_test', function() {
var product_id = $(this).data('id');
var thisproduct = $(this);
/* solution to undefined msie */
jQuery.browser = {};
jQuery.browser.msie = false;
jQuery.browser.version = 0;
if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) {
jQuery.browser.msie = true;
jQuery.browser.version = RegExp.$1;
}
/* solution to undefined msie */
jConfirm('Are you sure you want to delete record?', 'Remove Product', function(r) {
if (r == true) {
$.ajax({
type: 'POST',
url: 'scripts/ajax/index.php',
data: {
method: 'removeproduct_fromcart',
id: product_id
},
dataType: 'json',
success: function(data) {
if (data.RESULT == 0) {
$(".price span").text(data.totalprice);
$(thisproduct).closest('tr').remove();
$(".cart-number").text(data.productcount - 1);
$.growl.notice({
title: "Shopping Cart",
message: data.MSG
});
location.reload();
// window.location.href = 'https://www.youth-revisited.co.uk/payment.html';
}
// window.location.href = 'https://www.youth-revisited.co.uk/payment.html';
}
});
// window.location.href = 'https://www.youth-revisited.co.uk/payment.html';
} else {
return false;
}
});
});
其他回答
美元的。jQuery 1.9已经删除了browser方法。
jQuery.browser()删除 jQuery.browser()方法自jQuery 1.3起已弃用,并在1.9中被移除。如果需要,它可以作为jQuery Migrate插件的一部分使用。我们建议在Modernizr这样的库中使用特征检测。 - jQuery Core 1.9升级指南。
如升级指南中所述,您可以尝试使用jQuery Migrate插件来恢复此功能,并让jQuery工具工作。
这是GitHub上的jQuery工具bug。你可以试试其中一个补丁。
edit -在我看来,jQuery工具并没有得到很多支持。我个人不会开始一个依赖于该库的新项目,除非我准备自己接管支持。
这个问题主要是由浏览器导航和版本属性引起的。 为此,你必须在函数中添加以下代码:
/* solution to undefined msie */
jQuery.browser = {};
jQuery.browser.msie = false;
jQuery.browser.version = 0;
if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) {
jQuery.browser.msie = true;
jQuery.browser.version = RegExp.$1;
}
/* solution to undefined msie */
例如,我正在使用它的点击功能,看到它:
$(document).on('click', '.remove_add_test', function() {
var product_id = $(this).data('id');
var thisproduct = $(this);
/* solution to undefined msie */
jQuery.browser = {};
jQuery.browser.msie = false;
jQuery.browser.version = 0;
if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) {
jQuery.browser.msie = true;
jQuery.browser.version = RegExp.$1;
}
/* solution to undefined msie */
jConfirm('Are you sure you want to delete record?', 'Remove Product', function(r) {
if (r == true) {
$.ajax({
type: 'POST',
url: 'scripts/ajax/index.php',
data: {
method: 'removeproduct_fromcart',
id: product_id
},
dataType: 'json',
success: function(data) {
if (data.RESULT == 0) {
$(".price span").text(data.totalprice);
$(thisproduct).closest('tr').remove();
$(".cart-number").text(data.productcount - 1);
$.growl.notice({
title: "Shopping Cart",
message: data.MSG
});
location.reload();
// window.location.href = 'https://www.youth-revisited.co.uk/payment.html';
}
// window.location.href = 'https://www.youth-revisited.co.uk/payment.html';
}
});
// window.location.href = 'https://www.youth-revisited.co.uk/payment.html';
} else {
return false;
}
});
});
我在使用JQuery 1.10和JQuery UI 1.8时得到这个错误。我能够通过更新到最新的JQuery UI 1.11.4来解决这个错误。
从Visual Studio更新JQuery UI的步骤:
导航到项目或解决方案 右键单击:“管理NuGet包” 在左侧,单击“Installed Packages”选项卡 找到“JQuery UI (Combined library)”,然后单击Update 如果找到,选择它并单击Update 如果没有找到,请在左侧的“Online > nuget.org”选项卡中找到它,然后单击安装。如果旧版本的Jquery UI版本仍然存在,可以将其从项目中删除
因为我不打算支持旧的IE版本,我只是替换了所有对浏览器的引用。我爱虚伪。 我知道这不是个好办法,但对我来说很管用。
(实际上,它们以!browser的形式出现。Msie,可以从条件中省略。)