SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast lauriek's Avatar
    Join Date
    Dec 2002
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get window position in js

    This is something I've been trying to do for a while now and I feel silly for not getting it working. I'm sure it should be possible.

    I want to find the position on the users screen of a popup window. I know I can position one where I want, but say the user then moves it, can I then get X-Y coordinates so I can know where the user has moved it? I can get the width and height without issue...

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    Yes you can but it involves checking which browser is being used!
    IE understands screenLeft and screenTop but not screenX and screenY which everything else does.

    so this should work cross browser!

    Code:
    <html>
       <head>
          <script type="text/javascript">
    
    function whereisit () {
    	if (navigator.appName.indexOf("Netscape")!=-1
    		&&parseInt(navigator.appVersion)>=5) {
    	     x = "Value of Left coordinate is: " + window.screenX;
             alert(x);
             y = "Value of Top coordinate is: " +  window.screenY;
             alert(y);
    	}		
    	else if (navigator.appName.indexOf("Microsoft")!= -1 
    	  &&parseInt(navigator.appVersion)>=4) {
    	     x = "Value of Left coordinate is: " + window.screenLeft;
             alert(x);
             y = "Value of Top coordinate is: " +  window.screenTop;
             alert(y);
    	}
    }
          </script>
       </head>
     
    
       <body>
           <input type="button" onclick="whereisit()"  value="Coordinate Value">
    
       </body>
    </html>
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Enthusiast lauriek's Avatar
    Join Date
    Dec 2002
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh huge thanks for that, that seems to work brilliantly!

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    No problem, glad I could help! Javascript isn't my strongest point but I am getting there!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •