IE: "Expected Identifier" error :(

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • trappedIntoCode
    New Member
    • May 2007
    • 5

    #1

    IE: "Expected Identifier" error :(

    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" .
    Code:
                
      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?
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    IE does not understand the pageX or pageX offset property

    Comment

    • trappedIntoCode
      New Member
      • May 2007
      • 5

      #3
      Originally posted by mrhoo
      IE does not understand the pageX or pageX offset property
      Yeah, well but the pageX and pageY are used for NS4, not IE, please notice the If--else blocks. I forgot to mention that I have already detected the browser versions. :)

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        See the compatibility tables for different browsers for the mouse position.

        It might also help to read the Find Position page.

        As far as browser sniffing is concerned, that's bad practice. Use object detection instead - see link

        Comment

        • trappedIntoCode
          New Member
          • May 2007
          • 5

          #5
          Thanks links acoder. I did try following:

          Code:
          if(ie){
          
                  document.this.idf["style.left"] = this.popLeft + document.body.scrollLeft;
                  document.this.idf["style.top"]=this.popTop;
              }
          and also

          Code:
          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:

          Code:
          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....... .
          anyways, problem is solved. :)

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Unfortunately, we have to live with that when programming in Javascript.

            Browser vendors sometimes add their own proprietary properties or just plainly do not follow the standards. Sometimes they make an attempt but fail. To top it all, Microsoft is the worst culprit and their browsers also happen to be the most popular.

            Anyway, glad you got your problem solved even though I may not entirely agree with the coding.

            Comment

            • trappedIntoCode
              New Member
              • May 2007
              • 5

              #7
              Yip, Microsoft seems to be the biggest culprit, their Corporate strategies have always overshadowed their Softwares (non-standard implementations , borrowing syntaxes (c# vs Java) etc.). Anyways.
              I may not entirely agree with the coding.
              ???
              Can you suggest a better way? eg. using Object Detection?

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by trappedIntoCode
                Can you suggest a better way? eg. using Object Detection?
                It's not that difficult. Read the article on Object Detection which I linked to earlier.

                Basically, replace if(ie), if(ns), etc. with detection of objects. So your code will be very similar - just that it will detect objects rather than browsers. This will make it future-proof. Many browser-sniffing programs/code fell apart with the introduction of IE7, because the code was checking for IE6, IE5, etc.

                So, in your code, for example, you check for pageXOffset. IE doesn't support it, so it executes the second code block instead. If future versions of IE do support it (I'm not sure about IE7), your code doesn't break.

                Comment

                • trappedIntoCode
                  New Member
                  • May 2007
                  • 5

                  #9
                  Originally posted by acoder
                  It's not that difficult. Read the article on Object Detection which I linked to earlier.

                  Basically, replace if(ie), if(ns), etc. with detection of objects. So your code will be very similar - just that it will detect objects rather than browsers. This will make it future-proof. Many browser-sniffing programs/code fell apart with the introduction of IE7, because the code was checking for IE6, IE5, etc.

                  So, in your code, for example, you check for pageXOffset. IE doesn't support it, so it executes the second code block instead. If future versions of IE do support it (I'm not sure about IE7), your code doesn't break.

                  Thanks acoder. I will try that. It sounds much better than browser detection.

                  Comment

                  Working...