jquery升级到新版本报错[jQuery] Cannot read property ‘msie’ of undefined错误的解决方法

简介:

最近把一个项目的jQuery升级到最新版,发现有些页面报错Cannot read property ‘msie’ of undefined。上jQuery网站上搜了一下,原因是$.browser这个api从jQuery1.9开始就正式废除,js代码里只要用到$.browser就会报这个错。具体说明参见jQuery官方说明

楼主顺便扩展阅读了一下,发现jQuery· 1.9之后的版本把所有在早期版本里标记为deprecated的api都正式删除了,不再向后兼容。对于升级到最新jQuery的童鞋而言,这break change意味着得花费一些额外的时间把迁移应用层代码到新api。幸运的是,jQuery团队考虑到了这个改动带来的不便,推出了一个插件jQuery Migrate,这个插件能够自动恢复那些在最新版本里之后被废弃的API,从而让已有的js应用层代码无须改动就能和最新的jQuery库一起正常运行。

下面是具体的解决方法,先下载最新的jQuery Migrate插件,然后在引用jQuery js的地方之后加上一行对jQuery Migrate js文件的引用即可。

<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.js"></script>

对于不喜欢用jQuery Migrate的童鞋,可以如下另外一种方法,注意确保下面代码的加载顺序在jQuery文件之后,$.browser的代码之前。

jQuery.browser={};(function(){jQuery.browser.msie=false; jQuery.browser.version=0;if(navigator.userAgent.match(/MSIE ([0-9]+)./)){ jQuery.browser.msie=true;jQuery.browser.version=RegExp.$1;}})();


原文地址:http://blchen.com/jquery-can-not-read-property-msie-of-the-undefined-error-solution/

相关文章
|
1月前
|
存储 JavaScript 前端开发
成功解决:Cannot read properties of undefined (reading ‘commit‘)
这篇文章提供了解决Vuex中"Cannot read properties of undefined (reading 'commit')"错误的两种方法:检查模板中的数据属性是否存在,以及确保在Vue实例中正确挂载了store对象。
成功解决:Cannot read properties of undefined (reading ‘commit‘)
|
1月前
|
定位技术 Apache
Echarts——Invalid geoJson format Cannot read property 'length' of undefined
Echarts——Invalid geoJson format Cannot read property 'length' of undefined
31 0
|
1月前
|
JavaScript
VUE——filemanager-webpack-plugin报错TypeError: Cannot read property 'isFile' of undefined
VUE——filemanager-webpack-plugin报错TypeError: Cannot read property 'isFile' of undefined
30 0
|
1月前
|
前端开发 JavaScript
VUE——Uncaught (in promise) TypeError: Cannot read property '__esModule' of undefined
VUE——Uncaught (in promise) TypeError: Cannot read property '__esModule' of undefined
27 0
TypeError: Cannot set properties of undefined (setting ‘resdata‘),res定义数据出现的问题,定义的方法用this换成that
TypeError: Cannot set properties of undefined (setting ‘resdata‘),res定义数据出现的问题,定义的方法用this换成that
Cannot read properties of undefined (reading ‘row‘)
Cannot read properties of undefined (reading ‘row‘)
Cannot read properties of undefined (reading ‘post‘)
Cannot read properties of undefined (reading ‘post‘)
|
2月前
|
JavaScript
element-ui Cannot read properties of undefined (reading ‘name‘),data中写成集合的形式
element-ui Cannot read properties of undefined (reading ‘name‘),data中写成集合的形式
|
2月前
|
JavaScript
Cannot read properties of undefined (reading ‘$router‘)
Cannot read properties of undefined (reading ‘$router‘)
Cannot read properties of undefined (reading ‘username‘),粗心惹的祸,数据都不知道写呢了,
Cannot read properties of undefined (reading ‘username‘),粗心惹的祸,数据都不知道写呢了,