Varun
Varun

Reputation: 5061

Ie7 javascript issue

I have following code its working one IE8, FF and other browser but is not working for IE7 on checking i found formatIncludes[i] is not working, any idea whats wrong in this with IE7 and any work around

var formatIncludes = valueToSet.replace(/[^\D]/g, '');
    for(var i=formatIncludes.length-1; i >= 0 ; i--){
        if(valueToSet && valueToSet != null && valueToSet.endsWith(formatIncludes[i])){
            valueToSet = valueToSet.substr(0, valueToSet.length - 1);
        }else{
            break;
        }
    }

String.prototype.endsWith = function(suffix) {
    return (this.indexOf(suffix, this.length - suffix.length) != -1);
};

formatIncludes can contain any string value

error on IE7

Message: 'length' is null or not an object
Line: 352
Char: 2
Code: 0
URI: http://localhost:7001/HHSPortal/framework/skeletons/hhsa/js/util.js

Upvotes: 0

Views: 239

Answers (1)

Ian
Ian

Reputation: 50933

It seems that indexing a string with [i] does not work? And the more consistent/correct solution is to use .charAt(i)

The reasoning can be found in these:

string.charAt(x) or string[x]?

JavaScript cross-browser: Is it safe to treat a string as array?

Upvotes: 3

Related Questions