PDA

View Full Version : IE: "Expected Identifier" error :(


trappedIntoCode
05-29-2007, 02:34 AM
Hi Everyone,
Here is a part of javascript code that works well in FF2 but shows above error in IE6 and error "missing name after . operator" in NS8

function PopUp(idf,stepX,stepY,speed){

this.idf=idf;
this.popXStep=stepX;
this.popYStep=stepY;
this.popSpeed=speed;
this.popLeft=0;
this.popTop=0;
};

PopUp.prototype.relocX=function() {
if(xon==0){this.popLeft=this.popLeft-this.popXStep;}
else{this.popLeft=this.popLeft+this.popXStep;}

if(this.popLeft<0){xon=1;this.popLeft=0;}
if(this.popLeft>=(chX-ohX)){xon=0;this.popLeft=(chX-ohX);}
if(ie){
window.alert("here!");
this.idf.style.left=this.popLeft+document.body.scrollLeft;
this.idf.style.top=this.popTop;
}
else if (ns4){
document.(this.idf).pageX=this.popLeft+window.pageXOffset;
document.(this.idf).pageY=this.popTop;
}
else if (ns6){
document.getElementById(this.idf).style.left=this.popLeft+window.pageXOffset
document.getElementById(this.idf).style.top=this.popTop
}

};


Here is the error copied from NS JS Console:
Error: missing name after . operator
Source File: file:///C:/templates/test.html
Line: 105, Column: 17
Source Code:
document.(this.idf).pageX=this.popLeft+window.pageXOffset;


Well it's not because of a reserved word use. Its an addition in my project,
I am generating a no. of pop ups on one page. Deadline is in 10 hrs! Can someone please help?

liorean
05-29-2007, 01:23 PM
document.(this.idf).pageXYou can't use an expression as the right hand side of a full stop. You need to use the hash notation:document[this.idf].pageX


However, do you even need the nn4 code today?

trappedIntoCode
05-29-2007, 06:19 PM
Thanks for your comment Liorean. I did try following:

if(ie){

document.this.idf["style.left"] = this.popLeft + document.body.scrollLeft;
document.this.idf["style.top"]=this.popTop;
}

and also

if(ie){

document["this.idf"].style.left = this.popLeft + document.body.scrollLeft;
document["this.idf"].style.top = this.popTop;
}

This time, IE showed no error, but did not behave as intended also.
THe two pop ups move n FF and NS8, but not in IE6. When I debugged with
.NET, I found out that "style" was undefined!
Then finally, I went back to basics:

if(ie){
document.getElementById(this.idf).style.left=this.popLeft+document.body.scrollLeft;
document.getElementById(this.idf).style.top=this.popTop+document.body.scrollTop;
}
else if (ns6){
document.getElementById(this.idf).style.left=this.popLeft+window.pageXOffset
document.getElementById(this.idf).style.top=this.popTop+window.pageYOffset
}


Now it works fine in NS8,IE6 and FF2!! and so I was able to sleep like a innocent kid who has just finished his exams.........:)
But I still dont know why document["this.idf"].style.left
did not work. Why there are so many compatibility problems? I am new to javascrip, I had been programming all my days with Java and VB/VC.
I have already used Prototype for one of my projects but because, its JS file was 57KB and I could use additional 30Kb of prototype. Hushshsh........

glenngv
05-30-2007, 10:54 PM
if(ie){

document.this.idf["style.left"] = this.popLeft + document.body.scrollLeft;
document.this.idf["style.top"]=this.popTop;
}

and also

if(ie){

document["this.idf"].style.left = this.popLeft + document.body.scrollLeft;
document["this.idf"].style.top = this.popTop;
}

But I still dont know why
document["this.idf"].style.left
did not work.
liorean posted this. Note that there are no quotes.
document[this.idf].pageX

But that still might not work with NS4. You need to use:
document.layers[this.idf].pageX

But as liorean said, you don't even need to code for NS4 today. Nobody uses NS4 nowadays.