943,880 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Jan 3rd, 2005
-1

javascript in mozilla firefox

Expand Post »
why does the event.clientX and event.clientY is working in IE but not work in firefox?

clientX and clientY shows the coordinate of the cursor relative to the screen
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
odee is offline Offline
27 posts
since Dec 2004
Jan 3rd, 2005
0

Re: javascript in mozilla firefox

Because the event object model is different in FireFox. Instead of using the IE Dom, you need to use the W3C Dom.

Let me know if you need more specific help.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Jan 18th, 2006
0

Re: javascript in mozilla firefox

So how to know the equivalent values in firefox ....

I'm having a requirement where I need to get the co-ordinates.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ramesh_karanam is offline Offline
2 posts
since Jan 2006
Jan 18th, 2006
0

Re: javascript in mozilla firefox

They are properties of the Event Object. From the www.w3.org:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. // Introduced in DOM Level 2:
  2. interface MouseEvent : UIEvent {
  3. readonly attribute long screenX;
  4. readonly attribute long screenY;
  5. readonly attribute long clientX;
  6. readonly attribute long clientY;
  7. readonly attribute boolean ctrlKey;
  8. readonly attribute boolean shiftKey;
  9. readonly attribute boolean altKey;
  10. readonly attribute boolean metaKey;
  11. readonly attribute unsigned short button;
  12. readonly attribute EventTarget relatedTarget;
  13. void initMouseEvent(in DOMString typeArg,
  14. in boolean canBubbleArg,
  15. in boolean cancelableArg,
  16. in views::AbstractView viewArg,
  17. in long detailArg,
  18. in long screenXArg,
  19. in long screenYArg,
  20. in long clientXArg,
  21. in long clientYArg,
  22. in boolean ctrlKeyArg,
  23. in boolean altKeyArg,
  24. in boolean shiftKeyArg,
  25. in boolean metaKeyArg,
  26. in unsigned short buttonArg,
  27. in EventTarget relatedTargetArg);
  28. };

So "event.clientX' should work, depending on how you are getting the "Event" object.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Jan 19th, 2006
0

Re: javascript in mozilla firefox

Thanks for the reply Greer...

But, my requirement is to show a pop-up if on close of the browser window. The below sample script is working well in IE but not in FireFox.

Could you please let me know the solution for Firefox .....

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html><head><title>Test</title>
  2. <script language ="Javascript">
  3.  
  4. function unLoadFnc(event)
  5. {
  6. if(window.event.clientX<-2000 && window.event.clientY<-2000)
  7. {
  8. alert("Closing the window .. ");
  9. }
  10. else
  11. {
  12. alert("Something else..");
  13. }
  14. }
  15.  
  16. </script>
  17. </head>
  18. <body onUnload="unLoadFnc(event)">
  19. Sample Html.
  20. </body>
  21. </html>

Regards,
Ramesh
Last edited by tgreer; Jan 19th, 2006 at 10:08 am. Reason: Added code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ramesh_karanam is offline Offline
2 posts
since Jan 2006
Jan 19th, 2006
0

Re: javascript in mozilla firefox

There is quite a bit wrong in your code. First, you absolutely don't need to measure any mouse position or x,y coordinates do what you've asked.

This will work to create an alert when the user closes the window:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!-- note: you're missing a doctype here! -->
  2. <html>
  3. <head>
  4. <title>Test</title>
  5. <script type="text/javascript">
  6.  
  7. function unLoadFnc()
  8. {
  9. alert("Closing the window .. ");
  10. }
  11. </script>
  12. </head>
  13. <body onUnload="unLoadFnc();">
  14. Sample Html.
  15. </body>
  16. </html>
  17.  

There's no need for events, clientX, any of that. Perhaps though, you're not showing me the entire script or explaining the entire project. So, in the case that you DO need the event, you need to test if it's part of the window object (IE) or not.

Here's how I would code it:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!-- doctype excluded, you need to pick one -->
  2. <html>
  3. <head>
  4. <title>Test</title>
  5.  
  6. <script type="text/javascript">
  7. function unLoadFnc(evt)
  8. {
  9. var e = (window.event) ? window.event : evt;
  10. alert(e.clientX);
  11. }
  12. </script>
  13. </head>
  14.  
  15. <body onUnload="unLoadFnc(event);">
  16.  
  17. Sample Html.
  18. </body>
  19. </html>

I hope you're noting a few things, such as the fact that your page needs a doctype declaration, the proper way to write a script tag, and the semicolon after your event handler assignment.

The goal is to pass in the event object, and in your function, create a variable to hold it, using the tertiary operator.

Note, in FireFox, if you ran this code, the alert would say "undefined". That's because the widget that closes the window (the 'X') isn't within the client, so clientX would be undefined.

You'd actually have to click something in the client area to get a value.

For example, a button:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!-- doctype excluded, you need to pick one -->
  2. <html>
  3. <head>
  4. <title>Test</title>
  5.  
  6. <script type="text/javascript">
  7. function unLoadFnc(evt)
  8. {
  9. var e = (window.event) ? window.event : evt;
  10. alert(e.clientX);
  11. }
  12. </script>
  13. </head>
  14. <body>
  15. Sample Html.
  16. <form>
  17. <input type="button" onclick="unLoadFnc(event);" />
  18. </form>
  19.  
  20. </body>
  21. </html>
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Apr 10th, 2006
0

Re: javascript in mozilla firefox

the problem with this page is that the event that you have fired happens even if the page is refreshed. and he does not what that to happen. he wants to call the event only when the browser window is shut

Quote originally posted by tgreer ...
There is quite a bit wrong in your code. First, you absolutely don't need to measure any mouse position or x,y coordinates do what you've asked.

This will work to create an alert when the user closes the window:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!-- note: you're missing a doctype here! -->
  2. <html>
  3. <head>
  4. <title>Test</title>
  5. <script type="text/javascript">
  6.  
  7. function unLoadFnc()
  8. {
  9. alert("Closing the window .. ");
  10. }
  11. </script>
  12. </head>
  13. <body onUnload="unLoadFnc();">
  14. Sample Html.
  15. </body>
  16. </html>
  17.  

There's no need for events, clientX, any of that. Perhaps though, you're not showing me the entire script or explaining the entire project. So, in the case that you DO need the event, you need to test if it's part of the window object (IE) or not.

Here's how I would code it:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!-- doctype excluded, you need to pick one -->
  2. <html>
  3. <head>
  4. <title>Test</title>
  5.  
  6. <script type="text/javascript">
  7. function unLoadFnc(evt)
  8. {
  9. var e = (window.event) ? window.event : evt;
  10. alert(e.clientX);
  11. }
  12. </script>
  13. </head>
  14.  
  15. <body onUnload="unLoadFnc(event);">
  16.  
  17. Sample Html.
  18. </body>
  19. </html>

I hope you're noting a few things, such as the fact that your page needs a doctype declaration, the proper way to write a script tag, and the semicolon after your event handler assignment.

The goal is to pass in the event object, and in your function, create a variable to hold it, using the tertiary operator.

Note, in FireFox, if you ran this code, the alert would say "undefined". That's because the widget that closes the window (the 'X') isn't within the client, so clientX would be undefined.

You'd actually have to click something in the client area to get a value.

For example, a button:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!-- doctype excluded, you need to pick one -->
  2. <html>
  3. <head>
  4. <title>Test</title>
  5.  
  6. <script type="text/javascript">
  7. function unLoadFnc(evt)
  8. {
  9. var e = (window.event) ? window.event : evt;
  10. alert(e.clientX);
  11. }
  12. </script>
  13. </head>
  14. <body>
  15. Sample Html.
  16. <form>
  17. <input type="button" onclick="unLoadFnc(event);" />
  18. </form>
  19.  
  20. </body>
  21. </html>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
unicorn11 is offline Offline
6 posts
since Mar 2006
Apr 10th, 2006
0

Re: javascript in mozilla firefox

Tgreer had more than one example.. if you read his comments and code you will learn much about event handling in mozilla.

To copy and paste, try the last example. to get the x and y coordinates, look at where tgreer posted the event object properties.

cheers.
Reputation Points: 20
Solved Threads: 5
Junior Poster
alpha_foobar is offline Offline
182 posts
since May 2005
Apr 10th, 2006
0

Re: javascript in mozilla firefox

wow, this thread is old...
Reputation Points: 20
Solved Threads: 5
Junior Poster
alpha_foobar is offline Offline
182 posts
since May 2005
Apr 10th, 2006
0

Re: javascript in mozilla firefox

Yeah... I wondered how/why it got bumped. No big deal.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Calculating Price in JavaScript
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: javascript to load images?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC