How can I rewrite the following to do the same thing but to also support trim() for IE.
var isAnonymous = settings.requester_name == null || settings.requester_name.trim() == "";
How can I rewrite the following to do the same thing but to also support trim() for IE.
var isAnonymous = settings.requester_name == null || settings.requester_name.trim() == "";
Try:
var isAnonymous = !settings.requester_name || !settings.requester_name.trim();
And you can use this polyfill:
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^[\s\xA0]+|[\s\xA0]+$/g, '');
};
}
In Javascript an empty string is a falsy value, so is null or undefined, given that the following should work
var isAnonymous = !settings.requester_name || !settings.requester_name.trim()
The second part of the expression converts the empty string into a boolean, which evaluates false if empty.
What version of IE are you using? Trim has been supported for a while...
If you are writing for older than ie9 you can add a polyfill for trim
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^[\s\xA0]+|[\s\xA0]+$/g, '');
};
}
FFR: Mozilla developer network is a great source for Javascript app docs, and they include polyfill implementations you can use for old browser support.
Heres the docs for trim (the polyfill may look familiar) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim