PROWAREtech








JavaScript: IE11 String.padEnd() Prototype/Function
Create the String.padEnd() prototype for Internet Explorer 11.
Still developing for IE11? Well, this prototype is used to pad the end of strings on IE11.
Note: this prototype requires the repeat prototype for IE11.
if (!String.prototype.padEnd) {
String.prototype.padEnd = function (targetLength, padString) {
targetLength = targetLength >> 0; //truncate if number or convert non-number to 0;
padString = String((typeof padString !== 'undefined' ? padString : ' '));
if (this.length > targetLength) {
return String(this);
}
else {
targetLength = targetLength - this.length;
if (targetLength > padString.length) {
padString += padString.repeat(targetLength / padString.length); //append to original to ensure we are longer than needed
}
return String(this) + padString.slice(0, targetLength);
}
};
}
Using the above code snippet:
if (!String.prototype.padEnd) {
String.prototype.padEnd = function (targetLength, padString) {
targetLength = targetLength >> 0; //truncate if number or convert non-number to 0;
padString = String((typeof padString !== 'undefined' ? padString : ' '));
if (this.length > targetLength) {
return String(this);
}
else {
targetLength = targetLength - this.length;
if (targetLength > padString.length) {
padString += padString.repeat(targetLength / padString.length); //append to original to ensure we are longer than needed
}
return String(this) + padString.slice(0, targetLength);
}
};
}
alert("123".padEnd(10, "#"));
Comment