I'm using a jQuery plugin Datatables to create my own views of information retrieved from a list and it is mostly working. Datatables is built on top of jQuery and relies on a lot of its features.

But a couple of different options or calls from datatables throw an error in the console:

Uncaught TypeError: this.replace is not a function...(init.js)

I've traced it down to jQuery and SharePoint both trying to polyfill String.trim().

The jQuery version $.trim(value) will return a string with the white spaces at beginning and end removed. If you pass it a number it just returns the number.

The SP version of is in init.js and this is it

String.prototype.trim = function() {

The problem with this is that the jQuery implementation isn't expecting that String.prototype.trim() will have been altered and so $.trim(number) will convert to number.replace(regex) and there is no replace method on numbers.

The init.js file loads before jQuery and I really don't want to have to completely rework jQuery.

Is there anyway to prevent SharePoint from messing up $.trim()?


So this is my really horrible hacky way of doing this that is really bad, but for the one page I need to get working today it seems to be working

   return this.valueof(this);

That puts a replace() method on numbers that just returns the value of the number. I'm sure there are soooo many reasons not to do this. If anybody has a better solution please let me know.

  • Worst part is that we don't have to worry about IE 8 anymore and all our browsers have a native String.trim() so the SP polyfill isn't even needed. – Rothrock Jan 12 at 0:30

I also asked this on Stackoverflow because it had to deal more with how Javascript deals with prototype and things like that. With the help of folks there I added this to my page:

String.prototype.trim = function(){
  if(typeof this.valueOf(this) === 'string'){
  } else {
    return this.valueOf(this);

$.trim = function(e){
    return String.prototype.trim.call(e);

Oddly I couldn't just replace the prototype or redefine $.trim(). I had to do both.

Your Answer

