Javascript: Internet Explorer 8 - trim() leads to 'Object doesn't support this property or method' error
We make use of the Javascript https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/trim function in our application but didn’t realise that it isn’t implemented by Internet Explorer until version 9.
This led to the following error on IE8 when we used it:
Message: Object doesn’t support this property or method Line: 18 Char: 13 Code: 0 URI: http://our.app/file.js
There’s a stackoverflow thread suggesting some different ways of implementing your own 'trim()' method but since we’re using jQuery already we decided to just use the '$.trim()' function from there.
Therefore:
var cleaned = ourString.trim();
becomes:
var cleaned = $.trim(ourString);
I’m sure I must have come across this before but I don’t remember when!
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.