Click to See Complete Forum and Search --> : problem with String.prototype


Caliban
09-15-2003, 05:49 PM
Hi y'all, JS gurues!

I have the following issue:

I execute a page which i defined two functions: lpad and rpad
using String.prototype, like this:
================
String.prototype.lPad = function (n,c) {var i; var a = this.split(''); for (i = 0; i < n - this.length; i++) {a.unshift (c)}; return a.join('')}

String.prototype.rPad = function (n,c) {var i; var a = this.split(''); for (i = 0; i < n - this.length; i++) {a.push (c)}; return a.join('')}
================
My PC has IE 5.5 installed.

But, in the laptop I'm working in right now (it's not mine), it's installed IE 5.0, and when the page reaches the sentence:

miRoot = "ENTERPRISES"; miRoot = miRoot.rPad(66,' ');

a message error is shown with the following msg:

"the object does not accept this property or method"

I isolate the error and i'm 100% sure there it is where is produced, because I tested the page, commenting all the references to xxxxx.rpad() and/or xxxxx.lpad(), and the page works fine, but the texts are not justified - that's the purpose of these li'l functions.

I think the problem is the IE version, but .... what do you have to say about it?


Thanx in advance.

Jona
09-15-2003, 06:03 PM
Interesting, indeed. Could you provide some code that shows the code in use?

[J]ona

Caliban
09-15-2003, 06:16 PM
... test it under IE 5.0 and IE 5.5+


T.I.A

************************

<HTML>
<HEAD>
<script LANGUAGE="javascript">
String.prototype.lPad = function (n,c) {var i; var a = this.split(''); for (i = 0; i < n - this.length; i++) {a.unshift (c)}; return a.join('')}
String.prototype.rPad = function (n,c) {var i; var a = this.split(''); for (i = 0; i < n - this.length; i++) {a.push (c)}; return a.join('')}
String.prototype.chunk = function (n,c){
var o; var i; var dv; var r;
o = '';
for (i=0; i< this.length; i++){
o = o + this.charAt(i);
dv = i; dv++;
r = dv % n;
if (r==0){ o=o+c; }
}
return (o);
}
String.prototype.trim = function (){while(''+this.charAt(0)==' '){this = this.substring(1,this.length);}return this;}

function rpad_sample(){
miText1 = "ENTERPRISE";
alert (miText1 + '<= This is the NON right-justified string!');
miText1 = miText1.rPad(66,' ');
alert (miText1 + '<= This is the right-justified string!');
}
</SCRIPT>
</HEAD>

<BODY onload='javascript:rpad_sample()'>
</BODY>
</HTML>

Jona
09-15-2003, 06:59 PM
I'm not sure, but perhaps unshift() and push() (as well as pop() and shift()) are the problem. These methods were implemented in JavaScript 1.2.

[J]ona