views:

4497

answers:

7

I tried to apply .trim() in JavaScript in one of my programs. It's working fine under Mozilla, but an error displays when I try it in IE8. Does anyone know what is going on here? Is there anyway I can make it work in IE?

code:

var ID = document.getElementByID('rep_id').value.trim();

error display: Message: Object doesn't support this property or method Line: 604 Char: 2 Code: 0 URI: http://test.localhost/test.js

+1  A: 

I don't think there's a native trim() method in the JavaScript standard. Maybe Mozilla supplies one, but if you want one in IE, you'll need to write it yourself. There are a few versions on this page.

JW
+6  A: 

https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/String/Trim

This is a pretty recent addition to javascript, and its not supported by IE.

Erik
+9  A: 

It looks like that function isn't implemented in IE. If you're using jQuery, you could use $.trim() instead (http://api.jquery.com/jQuery.trim/).

jrummell
Now, I love jQuery, but importing it just for .trim() seems overkill
Erik
I agree. That's why I said "if you're using jQuery ..." =)
jrummell
Thanks! I didn't know that jQuery shipped with a trim method! But then again I didn't know that IE didn't implement this in the first place :p
Kimble
Alas, at the time of writing, IE 8 doesn't like jQuery's trim method.
James
+20  A: 

Add the following code to add trim functionality to the string.

if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, ''); 
  }
}
Ben Rowe
Good answer. Note that `replace(/^\s\s*/, '').replace(/\s\s*$/, '')` should be about 3 times faster than `replace(/^\s+|\s+$/, '')` in Firefox 2, according to one benchmark: http://blog.stevenlevithan.com/archives/faster-trim-javascript
Daniel Vassallo
Thanks :) I'll update my repo
Ben Rowe
Note also that `replace(/^\s+|\s+$/, '')` only removes either leading or trailing spaces, which is not the behavior expected from a trim function. If you want to remove both leading and trailing spaces you need to use `replace(/^\s+|\s+$/g, '')`.
Massimiliano Fliri
Thanks. I've updated my answer
Ben Rowe
+2  A: 

Unfortunately there is not cross browser JavaScript support for trim().

I currently use the following methods to add trim support to strings (lifted from where I can't remember).

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}
Iain Collins
A: 
var res = function(str){var ob;var oe;for(var i = 0; i < str.length; i++){if(str.charAt(i) != " " && ob == undefined){ob = i;}if(str.charAt(i) != " "){oe = i;}}return str.substring(ob,oe+1);

}

Dhaval dave
A: 

I had a similar issue when trying to trim a value from an imput and then ask if it was equal to nothing:

if ($(this).val().trim() == "")

However this threw a spanner in the works for IE6 - 8. Annoyingly enough I'd tried to var it up like so:

   var originalValue = $(this).val();

So I used the following, which works perfectly for me in all browsers...

var originalValueTrimmed = $.trim($(this).val());              
            if (originalValueTrimmed  == "") { ... }
kaichanvong