473,788 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

feature detection to replace use of "navigator.user Agent" in Yahoo! UI event.js?

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 scrolled to the right. What would be the appropriate feature
detection to use for the conditional in the following line?

if ( this.isIE ) {

Thank you,
Peter
isSafari: (/Safari|Konquero r|KHTML/gi).test(naviga tor.userAgent),

isIE: (!this.isSafari && !navigator.user Agent.match(/opera/gi) &&
navigator.userA gent.match(/msie/gi)),
getPageX: function(ev) {
var x = ev.pageX;
if (!x && 0 !== x) {
x = ev.clientX || 0;

if ( this.isIE ) {
x += this._getScroll ()[1];
}
}
return x;
},

_getScroll: function() {
var dd = document.docume ntElement, db = document.body;
if (dd && dd.scrollTop) {
return [dd.scrollTop, dd.scrollLeft];
} else if (db) {
return [db.scrollTop, db.scrollLeft];
}
return [0, 0];
}

Sep 1 '06 #1
10 2193
pe**********@gm ail.com wrote:
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 scrolled to the right. What would be the appropriate feature
detection to use for the conditional in the following line?

if ( this.isIE ) {
Don't use browser sniffing at all, use the stuff from quirksmode for
detecting the mouse coordinates:

<URL: http://www.quirksmode.org/js/events_properties.html >
Matt Kruse also has a utilities library to do much the same thing:

<URL: http://www.javascripttoolbox.com/lib/util/ >
[...]

--
Rob

Sep 1 '06 #2

RobG wrote:
pe**********@gm ail.com wrote:
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 scrolled to the right. What would be the appropriate feature
detection to use for the conditional in the following line?

if ( this.isIE ) {

Don't use browser sniffing at all, use the stuff from quirksmode for
detecting the mouse coordinates:

<URL: http://www.quirksmode.org/js/events_properties.html >
Matt Kruse also has a utilities library to do much the same thing:

<URL: http://www.javascripttoolbox.com/lib/util/ >
Hi Rob,

Thanks for the links. The quirksmode link gives a nice, concise
solution. Trusting that it is a robust way of doing things, I ended up
with a very short function

getPageX: function(e) {
if (e.pageX) {
return e.pageX;
} else if (e.clientX) {
return e.clientX + document.body.s crollLeft +
document.docume ntElement.scrol lLeft;
}
},

What to do in the case neither e.pageX nor e.clientX exist? Probably
will never happen?

Thanks again!
Peter

Sep 1 '06 #3

petermich...@gm ail.com wrote:
RobG wrote:
[...]
Don't use browser sniffing at all, use the stuff from quirksmode for
detecting the mouse coordinates:

<URL: http://www.quirksmode.org/js/events_properties.html >
Matt Kruse also has a utilities library to do much the same thing:

<URL: http://www.javascripttoolbox.com/lib/util/ >

Hi Rob,

Thanks for the links. The quirksmode link gives a nice, concise
solution. Trusting that it is a robust way of doing things, I ended up
with a very short function

getPageX: function(e) {
if (e.pageX) {
return e.pageX;
} else if (e.clientX) {
return e.clientX + document.body.s crollLeft +
document.docume ntElement.scrol lLeft;
}
},
I like use an ePos() function that returns an object with x & y
properties that are the event co-ords - but that's just my preference.
Then I can test the returned value simply:

var eventXY = ePos(event);
if (ePos){...}

If you just return a value, then to account for the fact that zero is a
valid return value you have to do something like:

var eventX = getPageX(event) ;
if ('number' == typeof eventX) {...}

What to do in the case neither e.pageX nor e.clientX exist? Probably
will never happen?
Never say never :-) Mobile browsers are becoming more prevelent, while
their support for JavaScript is generally great, their scriptable DOM
support leaves a lot to be desired.

According to Quirksmode, pageX/clientX goes back to IE 5 and Navigator
4, hopefully that is far enough for you. Anyhow, return some value you
can recognise as "not supported" (say null) and deal with it in the
calling function.

if ('number' == typeof eventX) {
/* do something with the returned value */
} else {
/* not supported, deal with it */
}
--
Rob

Sep 1 '06 #4
pe**********@gm ail.com wrote:
RobG wrote:
pe**********@gm ail.com wrote:
<snip>
>>What would be the appropriate feature
detection to use for the conditional in the following line?

if ( this.isIE ) {
Don't use browser sniffing at all, use the stuff from quirksmode for
detecting the mouse coordinates:

<URL: http://www.quirksmode.org/js/events_properties.html >
Matt Kruse also has a utilities library to do much the same thing:

<URL: http://www.javascripttoolbox.com/lib/util/ >

Hi Rob,

Thanks for the links. The quirksmode link gives a nice, concise
solution. Trusting that it is a robust way of doing things, I ended up
with a very short function

getPageX: function(e) {
if (e.pageX) {
return e.pageX;
} else if (e.clientX) {
return e.clientX + document.body.s crollLeft +
document.docume ntElement.scrol lLeft;
}
},

What to do in the case neither e.pageX nor e.clientX exist?
Probably will never happen?
That may never happen, but a browser providing - pageX - only can still
have its value at zero and so return undefined. It is also possible
that the code may be exposed to a browser where - clientX - is not
accompanied by both of - body - and - documentElement -, so error out,
or that both of - body - and - documentElement - may be returning the
same scroll value for the browser (that was certainly true of some
Opera 7 releases, and usually worked as most scripts employed the first
of the two that appeared to provide a value so it did not matter if
both did).

(Opera <= 6 also had a bug in that they only supported - clientX/Y - on
events but returned page related offsets instead of window related).

Probably a better strategy might be to stop thinking in terms of
getting scroll positions from objects at the point of working out mouse
offsets from the page but instead to use a reliable cross-browser
scroll-offset reporting method and only apply it when an event only has
- clientX/Y - available.

<URL:
http://jibbering.com/faq/faq_notes/n....html#bdScroll >

Richard.

Sep 1 '06 #5

Richard Cornford wrote:
pe**********@gm ail.com wrote:
RobG wrote:
pe**********@gm ail.com wrote:
<snip>
>What would be the appropriate feature
detection to use for the conditional in the following line?

if ( this.isIE ) {
>
Don't use browser sniffing at all, use the stuff from quirksmode for
detecting the mouse coordinates:
>
<URL: http://www.quirksmode.org/js/events_properties.html >
>
>
Matt Kruse also has a utilities library to do much the same thing:
>
<URL: http://www.javascripttoolbox.com/lib/util/ >
Hi Rob,

Thanks for the links. The quirksmode link gives a nice, concise
solution. Trusting that it is a robust way of doing things, I ended up
with a very short function

getPageX: function(e) {
if (e.pageX) {
return e.pageX;
} else if (e.clientX) {
return e.clientX + document.body.s crollLeft +
document.docume ntElement.scrol lLeft;
}
},

What to do in the case neither e.pageX nor e.clientX exist?
Probably will never happen?

That may never happen, but a browser providing - pageX - only can still
have its value at zero and so return undefined. It is also possible
that the code may be exposed to a browser where - clientX - is not
accompanied by both of - body - and - documentElement -, so error out,
or that both of - body - and - documentElement - may be returning the
same scroll value for the browser (that was certainly true of some
Opera 7 releases, and usually worked as most scripts employed the first
of the two that appeared to provide a value so it did not matter if
both did).

(Opera <= 6 also had a bug in that they only supported - clientX/Y - on
events but returned page related offsets instead of window related).

Probably a better strategy might be to stop thinking in terms of
getting scroll positions from objects at the point of working out mouse
offsets from the page but instead to use a reliable cross-browser
scroll-offset reporting method and only apply it when an event only has
- clientX/Y - available.

<URL:
http://jibbering.com/faq/faq_notes/n....html#bdScroll >
Hi Richard,

Thanks for the info and the link.

I really did think that the quirksmode way of assuming document.body
and document.docume ntElement both exist seems a little suspicious.

The link you sent is an interesting approach. It seems a bit convoluted
but looks like it must be very efficient in use as it doesn't have to
if-else with each call. Is this code your work?

Thanks again,
Peter

Sep 1 '06 #6
Richard Cornford wrote:
Probably a better strategy might be to stop thinking in terms of
getting scroll positions from objects at the point of working out mouse
offsets from the page but instead to use a reliable cross-browser
scroll-offset reporting method and only apply it when an event only has
- clientX/Y - available.
Looking in Flanagan's book (4th ed p383), It looks like pageX/Y in
Netscape 4 would also need the scroll amounts added.

<URL:
http://jibbering.com/faq/faq_notes/n....html#bdScroll >

Using the function on jibbering, how does this function look?

getPageX: function(e) {
var x;
if (e.pageX) {
x=e.pageX;
} else if (e.clientX) {
x=e.clientX
} else {
return null;
}
return x + scrollInterface .getScrollX();
},
I wanted to cut this down to something like the following but I
couldn't get it working

getPageX: function(e) {
var x = (e.pageX) ? e.pageX : (e.clientX) ? e.clientX : return
null;
return x + scrollInterface .getScrollX();
},
I'm still surprised that a little tasks like finding an event location
is so challenging.

In fact, I don't really care about supporting Netscape 4 with it's lack
of clientX. Similarly, I don't really care about supporting browsers
without document.getEle mentById().

Thanks,
Peter

Sep 1 '06 #7
pe**********@gm ail.com wrote:
<snip>
Using the function on jibbering, how does this function
look?

getPageX: function(e) {
var x;
if (e.pageX) {
You have repeated one of the logical mistakes in the Yahoo library and
failed to consider that a - pageX - may legitimately be numeric zero,
which type-converts to boolean false;
x=e.pageX;
} else if (e.clientX) {
x=e.clientX
} else {
return null;
NaN is the sensible value to return to indicate a failure from a
function call that would otherwise return another numeric value, and
the - isNaN - function is available for performing a discriminating test
for failure on the result.
}
return x + scrollInterface .getScrollX();
If the event object has a - pageX - property you don't want to be adding
scroll offsets to the - x - value. That addition should be restricted to
the - if(typeof e.clientX == 'number') - branch (if not more guarded).
},
I wanted to cut this down to something like the following but I
couldn't get it working

getPageX: function(e) {
var x = (e.pageX)?e.pag eX:(e.clientX)? e.clientX:retur n null;
<snip>

The - return - keyword forms a Statement, while the conditional operator
takes Expressions as its operands. A syntax error would be expected
here.

return (
(typeof e.pageX == 'number')?
e.pageX:
(
(typeof e.clientX == 'number')?
(
e.clientX + scrollInterface .getScrollX()
):
NaN
)
);

- would be the single Statement formulation of your code.
I'm still surprised that a little tasks like finding an event
location is so challenging.
You can rise to a challenge or hide from it. Why do you think the
general standard of scripting on the Internet is so poor?
In fact, I don't really care about supporting Netscape 4
with it's lack of clientX.
Why would that be a problem as it supports - pageX -? The code you have
written will work fine with Netscape 4, so long as the - pageX - is not
zero.
Similarly, I don't really care about supporting
browsers without document.getEle mentById().
That attitude will get in the way of your learning browser scripting.
Learn to design systems that still makes sense (and in e-commerce terms;
makes money) with client-side scripting disabled and you will find
yourself in a position to cope regardless of whether a browser
provides - document.getEle mentById - or not. And in a position to make
informed decisions about when designing/creating such a system is
appropriate.

Richard.
Sep 7 '06 #8
pe**********@gm ail.com wrote:
<snip>
I really did think that the quirksmode way of assuming
document.body and document.docume ntElement both exist
seems a little suspicious.
I am not a fan of the quirksmode attitude towards browser scripting.
There seems to be a lot of throwing up of hands in horror and then
walking away from the problems.
The link you sent is an interesting approach. It seems
a bit convoluted
It is convoluted, but internal complexity inside self-contained
components that follow the logic of the problem through to the point
where they should not need ongoing maintenance is not a great concern of
mine. The external interface is simple and that should be the only
concern of anyone using the component.
but looks like it must be very efficient in use as it
doesn't have to if-else with each call.
Yes, I don't like repeating tests on each call to a function in an
environment/context where all tests after the first a bound to return
the same result as the first.
Is this code your work?
Would that matter?

Richard.
Sep 7 '06 #9
Hi Richard,

Thank you for the detailed reply.

[snip]
I'm still surprised that a little tasks like finding an event
location is so challenging.

You can rise to a challenge or hide from it. Why do you think the
general standard of scripting on the Internet is so poor?
I definitely want to rise to the challenge. A lot of the browser
incompatibility workarounds are quite subtle and it's great exprienced
people here are willing to share their experience. I also think that
the prototype.js, Scriptaculous, Yahoo! UI developers should spend more
time here asking quesitons and reading the discussions. Perhaps they
have to maintain an appearance of expertise.

Peter

Sep 7 '06 #10

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

Similar topics

60
7309
by: Fotios | last post by:
Hi guys, I have put together a flexible client-side user agent detector (written in js). I thought that some of you may find it useful. Code is here: http://fotios.cc/software/ua_detect.htm The detector requires javascript 1.0 to work. This translates to netscape 2.0 and IE 3.0 (although maybe IE 2.0 also works with it)
7
6544
by: Larry R Harrison Jr | last post by:
I am looking for javascript and a basic tutorial on how to make mouse-over drop-down menus--the type that when you "hover" over a subject links relevant to that subject "emerge" which you can then "hover" over and click. (see the links left on http://www.dpreview.com to see what I mean) I have code from smartwebby.com (DHTML) but I'm not sure if it's the best, and I'm not sure how to integrate any menus of my own into it. The code...
1
5909
by: OtisUsenet | last post by:
Hi, I have a bookmarklet that works perfectly in Firefox, IE, Konqueror, and Opera, but in Safari 2.0.3 (417.9.2) it doesn't work. I enabled debugging and I can see "TypeError - Undefined value" reported in the Javascript console, but I'm not sure where the error is. The bookmarklet in question is here: javascript:if (parseInt(navigator.appVersion) >= 4) { if
7
5344
by: Gabriella | last post by:
Hi, I would like to know how to find out which is the browser's "home" URL? This is so I'll be able to suggest "set as homepage" for my website, only for those who did not set it beforehand. I know there's a security issue with this, but is there some workaround it? Thanks,
4
2050
by: rebeccatre | last post by:
please help me with this, <style> #a, option.message {background-color: green; color: white;} #b, option.message {background-color: yellow; color: black;} </style> <select id="thisselect"> <option id="thisone" class="message">THANK YOU</option> </select>
30
4702
by: kj | last post by:
My book (Flanagan's JavaScript: The Definitive Guide, 5th ed.) implies on page 111 that the following two constructs are equivalent: ( x.constructor == Foo ) and ( x instanceof Foo ) The author states that the instanceof operator computes its result
26
3905
by: Jake Barnes | last post by:
I did a search on the newsgroup comp.lang.javascript. I was searching for "how to play a sound with Javascript". I'm somewhat suprised that the majority of entries are from the 1990s, and there are almost no posts from the last 3 years: http://groups.google.com/group/comp.lang.javascript/search?group=comp.lang.javascript&q=how+to+play+a+sound+with+Javascript&qt_g=Search+this+group Even after sorting by date, there don't appear any...
24
8228
by: Jeremy J Starcher | last post by:
While reading c.l.j, I've noticed that some people prefer and indeed even recommend the use of "window.alert()" over "alert()". I can't find any technical reason to make this distinction, and seems to have a (tiny) amount overhead since window itself points to the global object, hence, a circular reference. (From everything I am reading, window is just a REFERENCE back to the global object, as opposed to a separate object.)
14
10901
by: terrible.theterrible4 | last post by:
Hello w3c,php, fedora + apache experts! Is there code I can use for POST A COMMENT? I am not ready for PHP yet. Anybody did it in JS?
33
11633
by: Sunny | last post by:
Hi, Sometime, when your script is too big, IE Gives you a warning "Stop Running This Script" A script on this page is causing Internet Explorer to run slowly. Does anyone knows, How to correct that? Thanks.
0
9498
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
10366
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
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
7517
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
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5399
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...
1
4070
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
3674
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.