473,797 Members | 3,212 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

event.clientX in Safari is not like in other browsers

Hi,

I just did a bunch of testing and was surprised with the results. If
anyone doesn't know this stuff here is what I found about
event.clientX.

The event.clientX property in Safari is in page coordinates. In IE,
Opera, Firefox event.clientX it is in window coordinates.

This means a lot when trying to determine event location in page
coordinates. It seems best to use the old event.pageX property first if
it is a number. This works in Safari. This is how the quirksmode org
and Yahoo! UI scripts work anyway but not Flanagan's fifth edtion.

If the browser doesn't have event.pageX or it isn't a number then
hopefully we can assume that if event.clientX exists and is a number
that clientX is in window coordinates. Then add the amount of page
scroll to the clientX value to get the page coordinate.

I don't like the "assume" parts of this but maybe this is where
straight feature detection breaks down. The feature may be there but
the feature doesn't work properly. Safari has at least a few instances
of this problem.

I can imagine that we need to test that not only every feature we want
to use exists but that it works properly also. Yikes!

Peter

Nov 12 '06 #1
5 4024
Peter Michaux wrote:
I just did a bunch of testing and was surprised with
the results. If anyone doesn't know this stuff here is
what I found about event.clientX.

The event.clientX property in Safari is in page coordinates.
In IE, Opera, Firefox event.clientX it is in window
coordinates.
That is an old and re-occurring bug. It was, for example, true of all
scriptable Opera browsers up to version 6.
This means a lot when trying to determine event location
in page coordinates. It seems best to use the old
event.pageX property first if it is a number.
And as the bug is not new it is already normal for people who know heat
they are doing to use pageX/Y in preference to clientX/Y+scrolling
whenever it is available.
This works in Safari. This is how the quirksmode
org and Yahoo! UI scripts work anyway but not Flanagan's
fifth edtion.
Flanagan 's book is only the least bad javascript book available, you
should not expect to learn good browser scripting practices from it,
only how javascript works in general at best.
If the browser doesn't have event.pageX or it isn't a
number
If it has the property it really should be a number.
then hopefully we can assume that if event.clientX exists
Why assume that?
and is a number
that clientX is in window coordinates.
You just made the wrong assumption for Opera <= 6.
Then add the amount of page
scroll to the clientX value to get the page coordinate.
If the event has no pageX/Y and does have a clientX/Y and the clientX/Y
is reporting client-area co-ordinates then adding the scroll offsets
will normalise the co-ordinates to the preferred page co-ordinates
(having adjusted for the borders of the 'root' element, if necessary
(else you get the 2 pixel discrepancy in default IE configurations) ).
I don't like the "assume" parts of this but maybe this
is where straight feature detection breaks down.
You are thinking of feature detection in terms of verifying the
existence of properties. In practice it has always involved verifying
significant aspects of the behaviour of browser object models, those are
also features of the browser.
The feature may be there but the feature doesn't work
properly.
Can you find a formal standard that says what values the clinetX/Y
properties of an event object are supposed to report? Without that there
is no meaning to "properly".
Safari has at least a few instances
of this problem.

I can imagine that we need to test that not only every
feature we want to use exists but that it works properly
also. Yikes!
Not every feature, but certainly the ones that are known to be
problematic. Of course that implies a necessity to know which features
are problematic, but practice in trying to write genuinely cross-browser
code, and properly testing it, will reveal the issues to the interested
student. And there is always reading comp.lang.javas cript and engaging
with the group.

Richard.
Nov 13 '06 #2
Hi Richard,

Richard Cornford wrote:
>
If the event has no pageX/Y and does have a clientX/Y and the clientX/Y
is reporting client-area co-ordinates
How is it possible to detect if the clientX number value is in page or
client-area coordinates?

then adding the scroll offsets
will normalise the co-ordinates to the preferred page co-ordinates
(having adjusted for the borders of the 'root' element, if necessary
(else you get the 2 pixel discrepancy in default IE configurations) ).
This 2 px business confused the heck out of me a few weeks back. What
are these 2 px and are they there for a reason?

It turned out that I didn't need to worry about these 2 px because the
value I was reading and the value I was setting were both working
within this 2 px discrepancy. When does this 2 px business cause
unexpected results or grief or does it always seem to cancel out?
Thank you,
Peter

Nov 13 '06 #3
Hi Richard,

Peter Michaux wrote:
>
Richard Cornford wrote:

If the event has no pageX/Y and does have a clientX/Y and the clientX/Y
is reporting client-area co-ordinates

How is it possible to detect if the clientX number value is in page or
client-area coordinates?
I can't imagine it is possible to look at just one number and see if it
is in page or client-area coordinates. So there must be a bit of
browser sniffing to know that Opera <=6 is involved. I have a gateway
conditional to my script that will only allow access if the browser
supports window.addEvent Listener or window.attachEv ent and my tests
show that the later was not supported until Opera 7. So this may be a
moot issue for me unless I learn of other browsers that have similar
differences. That said, I still feel uncomfortable that another browser
might appear that has problems.

then adding the scroll offsets
will normalise the co-ordinates to the preferred page co-ordinates
(having adjusted for the borders of the 'root' element, if necessary
(else you get the 2 pixel discrepancy in default IE configurations) ).

This 2 px business confused the heck out of me a few weeks back. What
are these 2 px and are they there for a reason?

It turned out that I didn't need to worry about these 2 px because the
value I was reading and the value I was setting were both working
within this 2 px discrepancy. When does this 2 px business cause
unexpected results or grief or does it always seem to cancel out?
In IE I am definitely using this coordinate system (clientX +
pageScrollX) as a fixed reference frame from which I make relative
comparisons of, say, the mouse position now and before. The absolute
values are not important so the 2px is not important. I am interested
to see if I can get away with this forever or not. From the positioning
experiements I have done it seems best to use comparisons whenever
possible to cancel out and/or avoid any browser weirdness.

I did find an old post or yours from 2003 where you were looking into
the 2px in detail. I was wondering if that little bevel in IE meant
anything. Reading your post it seems you decided those were the 2
pixels in question.

http://groups.google.com/group/comp....44f181aac86c37

Thank you,
Peter

Nov 13 '06 #4
Peter Michaux wrote:
Peter Michaux wrote:
>Richard Cornford wrote:
>>>
If the event has no pageX/Y and does have a clientX/Y
and the clientX/Y is reporting client-area co-ordinates

How is it possible to detect if the clientX number value
is in page or client-area coordinates?

I can't imagine it is possible to look at just one number
and see if it is in page or client-area coordinates.
If you going to normalize client co-ordinates into page co-ordinates
then you are not dealing with just one number.

Remember that, for example, when the corresponding scroll value is zero
the difference between client-area and page co-ordinates is zero. So
some of the time you don't need to know the difference, and as soon as
you do need to know you have more information to make the judgement on
(i.e. if the clientX/Y is smaller than the scrollLeft/Top then they must
be client-area co-ordinates not page co-ordinates).

It is also the case that you only need to determine whether clientX/Y
are page or client-area co-ordinates with certainty once, as they will
not change within a single browser execution environment.
So there must be a bit of
browser sniffing to know that Opera <=6 is involved.
Get some practice applying reasoning to you browser scripting problems
and you won't see the need to retreat to browser sniffing quite often.
I have a gateway conditional to my script that will only
allow access if the browser supports window.addEvent Listener
or window.attachEv ent and my tests show that the later was not
supported until Opera 7.
That is 'object inference'.
So this may be a moot issue for me unless I learn of
other browsers that have similar differences.
You can arrange to never learn of other browsers with similar issues by
the simple expedient of never looking (that is quite a popular strategy
;)
That said, I still feel uncomfortable that another
browser might appear that has problems.

>>then adding the scroll offsets will normalise the
co-ordinates to the preferred page co-ordinates
(having adjusted for the borders of the 'root' element,
if necessary (else you get the 2 pixel discrepancy in
default IE configurations) ).

This 2 px business confused the heck out of me a few weeks
back. What are these 2 px
The borer on the 'root' element (the root element varies depending on
the rendering mode in IE 6+). The default in 2px (at least on 'classsic'
windows), but can be modified by adjusting the window borders
configuration on windows.
>and are they there for a reason?
Things would have been easier if hey had never been manifest in the IE
DOM.
>It turned out that I didn't need to worry about these 2 px
because the value I was reading and the value I was setting
were both working within this 2 px discrepancy.
When the difference is what is important then consistent discrepancies
cancel themselves out. If you intend to relate the absolute position of
elements on a page with mouse co-ordinates then it is necessary to
account for the discrepancy.
>When does this 2 px business cause
unexpected results or grief or does it always seem to cancel out?

In IE I am definitely using this coordinate system (clientX +
pageScrollX) as a fixed reference frame from which I make relative
comparisons of, say, the mouse position now and before.
<snip>

If you are only interested in the differences between, say mousedown and
mouseup events in the browser window (without the page being scrolled or
the window moved in-between) then screenX/Y co-ordinates are sufficient
for the task. Then no normalisation is required at all.

Richard.
Nov 13 '06 #5
Richard Cornford wrote:
Peter Michaux wrote:
then hopefully we can assume that if event.clientX exists

Why assume that?
and is a number
that clientX is in window coordinates.

You just made the wrong assumption for Opera <= 6.
Do you know of any browsers other than Opera <=6 where clientX is not
in window (ie client-area) coordinates?

Can you find a formal standard that says what values the clinetX/Y
properties of an event object are supposed to report? Without that there
is no meaning to "properly".
At least I am in good company with this error. :)

http://groups.google.com/group/comp....44f181aac86c37

I can imagine that we need to test that not only every
feature we want to use exists but that it works properly
also. Yikes!

Not every feature, but certainly the ones that are known to be
problematic.
I agree with this pragmatic approach but this approach results in
somewhat brittle scripts that may break in a future browser. If the
developer knows all problematic features in all existing browsers then
the resulting scripts would not be very brittle but still brittle to
some degree. So with this approach, which is probably the only
practical one with even moderately complex scripts, there is no way to
proclaim a script is cross-browser. The best possible claim would be
multi-browser where multi means all browsers in existence and known to
the developer.

If you going to normalize client co-ordinates into page co-ordinates
then you are not dealing with just one number.

Remember that, for example, when the corresponding scroll value is zero
the difference between client-area and page co-ordinates is zero. So
some of the time you don't need to know the difference, and as soon as
you do need to know you have more information to make the judgement on
(i.e. if the clientX/Y is smaller than the scrollLeft/Top then they must
be client-area co-ordinates not page co-ordinates).
This example doesn't seem to be enough information in all cases. If the
browser window is scrolled only one pixel then this approach wouldn't
work. I imagine you have other plans if needed.

I can see setting up a very elaborate grid of invisible divs 1px wide
or tall with known positions and comparing the mouseover event
coordinates with the known position of the div in question.

When the window is scrolled, I could try to place a little invisible
div under the mouse based on the clientX information alone. If the next
mousemove is over the little div then clientX is in page coordinates.

This seems ridiculous and probably is especially in light of your
earlier pragmatic advice: since Opera <=6 cannot use my script and if
all other browsers without pageX and with clientX give clientX in
client-area coordinates then I don't need to worry or go to such great
lengths.

You can arrange to never learn of other browsers with similar issues by
the simple expedient of never looking (that is quite a popular strategy
;)
It is an endless stream of browser installations on my computer. It is
all interesting but it definitely is a very time consuming task to
learn to write quality browser scripts. And learning is likely an
unending process.

I can understand the appeal of the Yahoo! A-grade browser list to
alleviate testing tedium and compatibility/bug issues.

http://developer.yahoo.com/yui/artic...ser-chart.html

The biggest problem with this list is the Yahoo! UI library's use of
the list. They really don't follow this graded browser strategy at all
except for the A-grade browsers. If I have a C-grade browser then parts
of the Yahoo! UI library will be defined even though they won't work. A
script based on Yahoo! UI could work up to a certain stage and then
break meaning the page is useless.

The list is also very limited and it is not apparent what they expect
to happen if another browser is used. The only conclusion I can make is
they don't care. Which means, for example, they don't care about Safari
1.3 which I think is strange. I know someone still using Mac IE 5.

Peter

Nov 13 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
21692
by: TheKeith | last post by:
Can someone tell me why this isn't working? <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script type="text/javascript"> function getcoords() { document.getElementById("coord-x").value = event.clientX + ""; document.getElementById("coord-y").value = event.clientY + "";
10
9818
by: Tom Szabo | last post by:
Is there an event when that triggers when the window is closing.... I am talking about when the user clicks on the cross on the right top corner of the window!!!
5
3063
by: code_wrong | last post by:
Hi Firefox does not support the IE specific 'event' identifier and just quits running the script. ..... even though the identifier only turns up in code marked for IE only ... Does this seem reasonable? The script is here: http://www.lunds.us/private/coolclock.htm
5
5216
by: chad.a.morris | last post by:
Hi everyone, I didn't know how to sum this up very well for the title, but hopefully the person(s) will come along who can help. I'm trying to use the window.event.clientX value for positioning a javascript routine, but I'm having trouble. When the javascript routine comes up, it comes up in a certain position based on window.event.clientX. However, the user could click on something in the script display (say, like a menu, or a...
10
2195
by: petermichaux | last post by:
Hi, I'm picking apart the Yahoo! UI event.js library and stripping it down to just what I need. I'm also trying to make the parts I use better. I'm stumped on how to fix the code for the getPageX() method. This method is supposed to give the same value in IE as other browsers for the event position relative to the left of the page. This value will be greater than the position relative to the left of the browser if the browser is...
5
4546
by: jaysonnward | last post by:
Hello All: I've recently been recreating some 'dropdown menus' for a website I manage. I'm writing all my event handlers into my .js file. I've got the coding to work in Firefox, but the onmouseleave / onmouseout event I've attached to my hidden drop down (in this case an <ul>), is not firing correctly. It seems that when the mouse enters the ul it fires the mouseleave event. The problem is in the hideMenu2(e) function. I'm copying...
1
2250
by: shiznit | last post by:
ClientX on safari gives the mouse coordinates relative to the whole document. Is there an solution for getting the mouse coordinates relative to the client's browser on safari?
11
2460
by: Stevo | last post by:
I've been using the unload event for a long time. I have this code, which I've abstracted and made into a stripped down simple test case below, and it works fine on the major browsers (IE5+, Firefox, Opera). It also works for all Safari versions before 3.1. It's as if they've deliberately made a change to prevent some actions in the unload handler. Has anyone heard of such a restriction? Here's the test case:
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9537
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9066
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7560
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3750
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.