jQuery IE9-10: unable to get property replace of u

2019-08-09 07:54发布

问题:

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)

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

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:

// 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.

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)?

回答1:

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.



回答2:

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



回答3:

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.

camelCase: function( string ) {
    if(!string){ 
         console.log("string is falsy", string);
         return string;
    }
    return string.replace( rmsPrefix, "ms-" ).replace( rdashAlpha, fcamelCase );
},