Javascript trim alternative for IE

You might have noticed that usage of trim in javascript often throws an error in IE, causing the javascript to break. There are two simple alternatives to this, to make it work perfectly all the browsers including IE. The primary concern for javascript trim is to delete all the trailing and leading spaces. For that:
  1. You can use dojo.string.trim() instead if using DOJO for your application
  2. You can use jQuery.trim() instead if using jQuery for your application
  3. In plain javascript, you can use str.replace(/^\s+|\s+$/g, “”)

The following snippet is worth giving a try to see it work:

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.