It isn't mine, but here it is:
<script language="JavaScript" type="text/javascript">
String.prototype.trim = function() {
/*This file retrieved from the JS-Examples archives
1000s of free ready to use scripts, tutorials, forums.
Author: Brock Weaver - 0*/
// skip leading and trailing whitespace
// and return everything in between
var x=this;
x=x.replace(/^\s*(.*)/, "$1");
x=x.replace(/(.*?)\s*$/, "$1");
return x;
}
</script>
use it like:
myStr = " happpy ";
alert ("--"+myStr.trim()+"--");