特征检测以替换“navigator.userAgent”的使用在雅虎! UI event.js? [英] feature detection to replace use of "navigator.userAgent" in Yahoo! UI event.js?

查看:58
本文介绍了特征检测以替换“navigator.userAgent”的使用在雅虎! UI event.js?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在挑选雅虎! UI event.js库并将其剥离

到我需要的东西。我也在努力使我使用的部件更好。

我很难解决如何修复getPageX()方法的代码。这个

方法应该在IE中提供与其他浏览器相同的值,相对于页面左侧的事件位置。如果

浏览器向右滚动,则该值将比浏览器左侧的位置大
。什么是适当的功能

检测用于以下行中的条件?


if(this.isIE){


谢谢,

彼得

isSafari:(/ Savari|Konqueror | KHTML / chi).test(navigator.userAgent),


isIE :(!this.isSafari&&!navigator.userAgent.match(/ opera / gi)&&

navigator.userAgent.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];

} < br $>
}

返回x;

},


_getScroll:function(){

var dd = document.documentElement,db = document.body;

if(dd&& dd.scrollTop){

return [dd。 scrollTop,dd.scrollLeft];

}否则如果(db){

返回[db.scrollTop,db.scrollLeft];

}

返回[0,0];

}

解决方案

< a href =mailto:pe ********** @ gmail.com> pe ********** @ gmail.com 写道:





我正在挑选雅虎! UI event.js库并将其剥离

到我需要的东西。我也在努力使我使用的部件更好。

我很难解决如何修复getPageX()方法的代码。这个

方法应该在IE中提供与其他浏览器相同的值,相对于页面左侧的事件位置。如果

浏览器向右滚动,则该值将比浏览器左侧的位置大
。什么是适当的功能

检测用于以下行中的条件?


if(this.isIE){



根本不要使用浏览器嗅探,使用quirksmode中的东西来检测鼠标坐标:


< URL: http://www.quirksmode.org/js/events_properties .html >

Matt Kruse还有一个实用程序库可以做同样的事情:


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

[...]


-

Rob


< blockquote>

RobG写道:

pe **********@gmail.com 写道:





我正在挑选雅虎! UI event.js库并将其剥离

到我需要的东西。我也在努力使我使用的部件更好。

我很难解决如何修复getPageX()方法的代码。这个

方法应该在IE中提供与其他浏览器相同的值,相对于页面左侧的事件位置。如果

浏览器向右滚动,则该值将比浏览器左侧的位置大
。什么是适当的功能

检测用于以下行中的条件?


if(this.isIE){



根本不要使用浏览器嗅探,使用quirksmode中的东西来检测鼠标坐标:
检测鼠标坐标:


< URL: http://www.quirksmode.org/js/events_properties .html >


Matt Kruse还有一个实用程序库可以做同样的事情:


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



你好Rob,


感谢您的链接。 quirksmode链接提供了一个漂亮,简洁的
解决方案。相信这是一种强有力的做事方式,我结束了

,功能很短


getPageX:function(e){

if(e.pageX){

返回e.pageX;

}否则if(e.clientX){

返回e.clientX + document.body.scrollLeft +

document.documentElement.scrollLeft;

}

},

如果e.pageX和e.clientX都不存在,该怎么办?可能

永远不会发生?


再次感谢!

Peter




petermich ... @ gmail.com写道:


RobG写道:


[...]


根本不要使用浏览器嗅探,使用来自quirksmode的东西

检测鼠标坐标:


< URL: http://www.quirksmode.org/js/events_properties.html >

Matt Kruse还有一个实用工具库可以做同样的事情:


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



你好Rob,


感谢您的链接。 quirksmode链接提供了一个漂亮,简洁的
解决方案。相信这是一种强有力的做事方式,我结束了

,功能很短


getPageX:function(e){

if(e.pageX){

返回e.pageX;

}否则if(e.clientX){

返回e.clientX + document.body.scrollLeft +

document.documentElement.scrollLeft;

}

},



我喜欢使用ePos()函数返回x&对象的对象y

属性是事件合作 - 但这只是我的偏好。

然后我可以简单地测试返回值:


var eventXY = ePos(event);

if(ePos){...}


如果你只返回一个值,然后考虑到零是一个有效的返回值,你需要做的事情:


var eventX = getPageX(event);

if(''number''== typeof eventX){...}


在这种情况下该做什么e.pageX还是e.clientX存在?可能

永远不会发生?



永远不要说永远:-)移动浏览器变得越来越普遍,而

他们对JavaScript的支持通常很棒,他们的脚本化DOM < b / b
支持还有很多不足之处。


根据Quirksmode,pageX / clientX回到IE 5和Navigator

4希望这对你来说已经足够了。无论如何,返回一些你的价值

可以识别为不支持 (比如null)并在

调用函数中处理它。


if(''number''== typeof eventX){

/ *用返回值做一些事情* /

}否则{

/ *不支持,处理它* /

}

-

Rob


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|Konqueror|KHTML/gi).test(navigator.userAgent),

isIE: (!this.isSafari && !navigator.userAgent.match(/opera/gi) &&
navigator.userAgent.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.documentElement, db = document.body;
if (dd && dd.scrollTop) {
return [dd.scrollTop, dd.scrollLeft];
} else if (db) {
return [db.scrollTop, db.scrollLeft];
}
return [0, 0];
}

解决方案

pe**********@gmail.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



RobG wrote:

pe**********@gmail.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.scrollLeft +
document.documentElement.scrollLeft;
}
},

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

Thanks again!
Peter



petermich...@gmail.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.scrollLeft +
document.documentElement.scrollLeft;
}
},

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


这篇关于特征检测以替换“navigator.userAgent”的使用在雅虎! UI event.js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆