javascript jQuery IE9-10:无法获取未定义或空引用的属性替换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32279630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery IE9-10: unable to get property replace of undefined or null reference
提问by Prefix
I'm developing a page with the following libraries;
我正在开发一个包含以下库的页面;
- jQuery (1.7.2) (older version because of a dependency issue, but have tried up to 1.9.1, doesn't fix issue).
- Backbone (1.1.0)
- lodash (2.4.1)
- modernizr (2.7.1)
- gsap (1.17.0)
- jQuery (1.7.2)(旧版本由于依赖问题,但已尝试到 1.9.1,没有解决问题)。
- 主干 (1.1.0)
- 洛达什 (2.4.1)
- 现代化 (2.7.1)
- gsap (1.17.0)
The page use canvas and gsap for animation. Everything works great in IE11, Chrome, Safari, Firefox, and IE8 (animations disabled for IE8), but IE9 and 10 just throw this error in the console over and
该页面使用画布和 gsap 来制作动画。在 IE11、Chrome、Safari、Firefox 和 IE8(IE8 禁用动画)中一切正常,但 IE9 和 10 只是在控制台中抛出此错误并
unable to get property 'replace' of undefined or null reference
无法获取未定义或空引用的属性“替换”
The line referenced is in jquery.js
, line 622, which is the return statement in this code:
引用的jquery.js
行在第 622 行,这是此代码中的 return 语句:
// Convert dashed to camelCase; used by the css and data modules
// Microsoft forgot to hump their vendor prefix (#9572)
camelCase: function( string ) {
return string.replace( rmsPrefix, "ms-" ).replace( rdashAlpha, fcamelCase );
},
I can't figure out how to determine what part of MY code caused this jQuery code to fire, so I'm unsure as to what may be the issue on my end.
我不知道如何确定我的代码的哪一部分导致了这个 jQuery 代码被触发,所以我不确定我最后可能是什么问题。
Does anyone know a fix for this? Or alternatively, how I can view what part of my code caused this jquery code to fire (using IE dev tools)?
有谁知道解决这个问题?或者,我如何查看代码的哪一部分导致此 jquery 代码触发(使用 IE 开发工具)?
采纳答案by Prefix
Turns out the issue wasn't something inherently wrong with IE, but rather I was trying to access an object which didn't entirely exist yet. The other browsers this was not an issue (perhaps their JS engines were just fast enough for it to be a non-issue), but I've now added checks to ensure all the relevant content has loaded before executing the problematic function and the issue seems to be gone. Thanks for the help, all.
事实证明,这个问题并不是 IE 固有的问题,而是我试图访问一个还不完全存在的对象。其他浏览器这不是问题(也许他们的 JS 引擎足够快以至于它不是问题),但我现在添加了检查以确保在执行有问题的功能和问题之前已加载所有相关内容好像没了。谢谢大家的帮助。
回答by Pax
What kind of tool are you using for debugging? I have a solution for you for Chrome debug console
1. First go Find the jquery script file in the console, right click and select "Blackbox script" it is going to be ignored while debugging, only your files are going to be considered
2. Activate break on errors and a breakpoint is going to be triggered on the line of code where the exception is occurring
Check JavaScript: Is there a way to get Chrome to break on all errors?
and https://developer.chrome.com/devtools/docs/blackboxing
您使用哪种工具进行调试?我有一个 Chrome 调试控制台的解决方案
1.首先在控制台中找到 jquery 脚本文件,右键单击并选择“黑盒脚本”它在调试时将被忽略,只有你的文件才会被考虑
2.激活错误时中断,将在发生异常的代码行上触发断点
检查JavaScript:有没有办法让 Chrome 中断所有错误?
和https://developer.chrome.com/devtools/docs/blackboxing
回答by SoluableNonagon
Maybe I'm wrong here but is it possible that IE8-9 "string" is a reserved word? Maybe not. But the only time .replace would show that message is if you were not feeding it a string.
也许我在这里错了,但 IE8-9“字符串”有可能是保留字吗?也许不会。但唯一一次 .replace 会显示该消息的是,如果您没有为其提供字符串。
camelCase: function( string ) {
if(!string){
console.log("string is falsy", string);
return string;
}
return string.replace( rmsPrefix, "ms-" ).replace( rdashAlpha, fcamelCase );
},