We help IT Professionals succeed at work.

Find Cursor Position In FireFox

shaggy_the_sheep
on
852 Views
Last Modified: 2008-01-09
Hi

I am using this code to find the position of the cursor, allowing users to drag windows around the screen.

function dragWindow(){
  x = (window.Event) ? e.pageX : event.clientX;
  y = (window.Event) ? e.pageY : event.clientY;

//DRAG CODE HERE....
  }

It works fine in IE, but if i try it in FireFox i get the error 'e is not defined' on line 444, which is this:
  x = (window.Event) ? e.pageX : event.clientX;

Any help would be greatly appeciated,

Richard
Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi

It works fine in IE, but it still isnt working in FireFox.

Richard

Commented:
Strange, my code above works fine in IE (6.0) and Firefox (1.5.0.4).

Try this just to check...

<html>
<head>
<title>Mouse Position</title>
<script type="text/JavaScript">
<!--

function dragWindow(evt){
     var evt = (evt)? evt : event;

     if(evt){
          var x = evt.clientX;
          var y = evt.clientY;

          return "X: " + x + " Y: " + y;
     }
}

//-->
</script>
</head>
<body>
<div style="border: 1px solid black; height: 40px; width: 40px;" onmousedown="alert(dragWindow(event))"></div>
</body>
</html>

If that works, then there might be another problem in your code, so maybe posting the resting would be helpful? :)

Author

Commented:
Yes your script works fine, must be something within my code.

What i am trying to do is allow the user to drag windows around my page. This is the part of the script that controls it:

var allowDrag = false;
function dragEmotion(){

var evt = (evt)? evt : event;

if(evt){
      var x = evt.clientX;
      var y = evt.clientY;
      }

alert('X:' + x + ' Y:' + y);
 
  if(allowDrag==true){
        document.getElementById('choose_emotion').style.top = y - 10;
        document.getElementById('choose_emotion').style.left = x - 10;
        if((x>-44&&x<189)&&(y>450&&y<555)){
              document.convo.colour.style.visibility = 'hidden';
              }
        else{
              document.convo.colour.style.visibility = 'visible';
              }
        if((x>-134&&x<121)&&(y>450&&y<555)){
              document.convo.font.style.visibility = 'hidden';
              }
        else{
              document.convo.font.style.visibility = 'visible';
              }
        if(x>document.body.clientWidth - parseInt(document.getElementById('choose_emotion').style.width) - 7){
              document.getElementById('choose_emotion').style.left = document.body.clientWidth - parseInt(document.getElementById('choose_emotion').style.width) - 10;
              }
        if(y>document.body.clientHeight - parseInt(document.getElementById('choose_emotion').style.height) - 7){
              document.getElementById('choose_emotion').style.top = document.body.clientHeight - parseInt(document.getElementById('choose_emotion').style.height) - 10;
              }  
      }
}

Now i have in the <body> tag onmousemove="dragEmotion(event)" to implement the script when the user moves the cursor.

This is the div that is moveable:

<div id="choose_emotion" style="position: absolute; top: 493px; left: 226px; width: 170px; height: 85px; background-color: #EEF3FA; border: 1px solid #909090; filter: alpha(Opacity=80); visibility: hidden; cursor: pointer;">
      <div style="position: absolute; top: 1px; left: 156px; cursor: pointer" onclick="document.getElementById('choose_emotion').style.visibility = 'hidden';" title="Close this dialog"><img src="http://www.fast-chat.co.uk/images/quit.png"></div>
      <div style="position: absolute; top: 1px; left: 0px; cursor: pointer; width: 18px; height: 10px; background: url(http://www.fast-chat.co.uk/images/drag.png); background-repeat: no-repeat; z-index: 0" onmousedown="allowDrag=true" onmouseup="allowDrag=false" title="Move the Emotion dialog"></div>
      <div style="background-color: #026AFE; text-align: center; color: white; font-family: Verdana; font-size: 7pt; cursor: default">Insert an Emoticon</div>
      <div id="list_emotion" style="text-align: center; margin-top: 3px; margin-bottom: -3px;">
      </div>
</div>

Hopefully this provides you with enough to find any errors, etc.

Just a thought, could it be the way that i am implementing the function that is wrong?

Thanks

Richard

Commented:
Change this line...

function dragEmotion(){

To this...

function dragEmotion(evt){

See if that works :)

Author

Commented:
Hi thanks

Sorry, missed that completely!! Works a treat now! Thanks for the quick response


Richard

Commented:
Anytime :)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.