String.prototype.trim

If your visitor base uses older browsers (IE8 and lower), the String.prototype.trim() method might not be available. If that is the case, you can use this polyfill (this version I believe was written by Douglas Crockford);

  1. if (!String.prototype.trim) {
  2. String.prototype.trim = function () {
  3. return this.replace(/^\s*(\S*(?:\s+\S+)*)\s*$/, "$1");
  4. };
  5. }

The regular expression used to trim the string, can be written in different ways. Here is another version, that also gets the job done;

  1. if (!String.prototype.trim) {
  2. String.prototype.trim = function () {
  3. return this.replace(/^\s+|\s+$/g, '');
  4. };
  5. }