473,725 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cross-browser setOpacity() without browser sniffing?

Hi,

Does anyone have a a cross-browser setOpacity function that does not
use browser sniffing? I looked at the Yahoo! UI function and it detects
IE by looking for window.ActiveXO bject. I also looked at Scriptaculous
and it uses navigator.userA gent.

Thanks,
Peter
Roughly the Yahoo! UI bit of relavent code

var el = document.getEle mentById('foo') ;

if (window.ActiveX Object && typeof el.style.filter == 'string') { //
in case not appended
el.style.filter = 'alpha(opacity= ' + val * 100 + ')';

if (!el.currentSty le || !el.currentStyl e.hasLayout) {
el.style.zoom = 1; // when no layout or cant tell
}
} else {
el.style.opacit y = val;
el.style['-moz-opacity'] = val;
el.style['-khtml-opacity'] = val;
}

The bit of Scriptaculous code

Element.setOpac ity = function(elemen t, value){
element= $(element);
if (value == 1){
Element.setStyl e(element, { opacity:
(/Gecko/.test(navigator .userAgent) &&
!/Konqueror|Safar i|KHTML/.test(navigator .userAgent)) ?
0.999999 : null });
if(/MSIE/.test(navigator .userAgent))
Element.setStyl e(element, {filter:
Element.getStyl e(element,'filt er').replace(/alpha\([^\)]*\)/gi,'')});
} else {
if(value < 0.00001) value = 0;
Element.setStyl e(element, {opacity: value});
if(/MSIE/.test(navigator .userAgent))
Element.setStyl e(element,
{ filter:
Element.getStyl e(element,'filt er').replace(/alpha\([^\)]*\)/gi,'') +
'alpha(opacity= '+value*100+')' });
}
}

Sep 4 '06 #1
16 2354
pe**********@gm ail.com wrote:
Hi,

Does anyone have a a cross-browser setOpacity function that does not
use browser sniffing? I looked at the Yahoo! UI function and it detects
IE by looking for window.ActiveXO bject. I also looked at Scriptaculous
and it uses navigator.userA gent.

Thanks,
Peter
Roughly the Yahoo! UI bit of relavent code

var el = document.getEle mentById('foo') ;

if (window.ActiveX Object && typeof el.style.filter == 'string') { //
in case not appended
el.style.filter = 'alpha(opacity= ' + val * 100 + ')';

if (!el.currentSty le || !el.currentStyl e.hasLayout) {
el.style.zoom = 1; // when no layout or cant tell
}
} else {
el.style.opacit y = val;
el.style['-moz-opacity'] = val;
el.style['-khtml-opacity'] = val;
}

The bit of Scriptaculous code

Element.setOpac ity = function(elemen t, value){
element= $(element);
if (value == 1){
Element.setStyl e(element, { opacity:
(/Gecko/.test(navigator .userAgent) &&
!/Konqueror|Safar i|KHTML/.test(navigator .userAgent)) ?
0.999999 : null });
if(/MSIE/.test(navigator .userAgent))
Element.setStyl e(element, {filter:
Element.getStyl e(element,'filt er').replace(/alpha\([^\)]*\)/gi,'')});
} else {
if(value < 0.00001) value = 0;
Element.setStyl e(element, {opacity: value});
if(/MSIE/.test(navigator .userAgent))
Element.setStyl e(element,
{ filter:
Element.getStyl e(element,'filt er').replace(/alpha\([^\)]*\)/gi,'') +
'alpha(opacity= '+value*100+')' });
}
}
Never played with opacity, but it seems it would work if you did
something like:
el.style.filter = 'alpha(opacity= ' + val * 100 + ')';
el.style.opacit y = val;
el.style['-moz-opacity'] = val;
el.style['-khtml-opacity'] = val;

Sep 5 '06 #2
pe**********@gm ail.com wrote:
Does anyone have a a cross-browser setOpacity function
that does not use browser sniffing? I looked at the Yahoo!
UI function and it detects IE by looking for
window.ActiveXO bject. I also looked at Scriptaculous and
it uses navigator.userA gent.
<snip>

You should not need to ask this question. The general principle of
feature detection is to devise a test that is as closely related
(preferably directly related) to the question that needs to be answered.
If your question is whether a browser supports IE-style filters then
test for the manifestation of filters; the - filters - collection of the
element:-

if(el.filters){
... //has filters collection.
}

- or the - filters - property of the style object:-

if(typeof el.style.filter s == 'string'){
...//has filters property on style object..
}

(the latter being the most direct test)

Richard.
Sep 5 '06 #3
Hi Richard,

Richard Cornford wrote:
pe**********@gm ail.com wrote:
Does anyone have a a cross-browser setOpacity function
that does not use browser sniffing? I looked at the Yahoo!
UI function and it detects IE by looking for
window.ActiveXO bject. I also looked at Scriptaculous and
it uses navigator.userA gent.
<snip>

You should not need to ask this question.
It seems like in the browser scripting world there are always suprises
due to bugs. I don't know about all the bugs. Also the Yahoo! UI guys
do know a thing or two and the fact that they double up with the
following two checks makes me wonder if something else is going on.

if (window.ActiveX Object && typeof el.style.filter == 'string')

The general principle of
feature detection is to devise a test that is as closely related
(preferably directly related) to the question that needs to be answered.
If your question is whether a browser supports IE-style filters then
test for the manifestation of filters; the - filters - collection of the
element:-

if(el.filters){
... //has filters collection.
}

- or the - filters - property of the style object:-

if(typeof el.style.filter s == 'string'){
...//has filters property on style object..
}

(the latter being the most direct test)
Richard, you have helped me much in the past months. I do appreciate it
greatly. Due to your persistance, I am currently trying to get myself
off the Yahoo! UI code and also ultimately rid my Rails code of
Prototype.js. I am moving to the layered library approach that you
described previously.

Thank you,
Peter

Sep 5 '06 #4
pe**********@gm ail.com wrote:
Richard Cornford wrote:
>pe**********@gm ail.com wrote:
>>Does anyone have a a cross-browser setOpacity function
that does not use browser sniffing? I looked at the Yahoo!
UI function and it detects IE by looking for
window.Active XObject. I also looked at Scriptaculous and
it uses navigator.userA gent.
<snip>

You should not need to ask this question.

It seems like in the browser scripting world there are always
suprises due to bugs. I don't know about all the bugs.
That does not men that most feature detection tests can be directly
deduced from pinning down the question that needs answering.
Also the Yahoo! UI guys
do know a thing or two
Whatever they may know it is not preventing their code form manifesting
non-joined up logic.
and the fact that they double up with the
following two checks makes me wonder if something
else is going on.

if (window.ActiveX Object && typeof el.style.filter == 'string')
Voodoo. Too many browsers have a global ActiveXObjet constructor (real
or object inference defeating dummy) for testing it in any context other
than a desire to instantiate an ActiveX object to be rational.

<snip>
>if(typeof el.style.filter s == 'string'){
<snip>

This should have been:-

if(typeof el.style.filter == 'string'){

Richard.
Sep 5 '06 #5
Richard Cornford wrote:
pe**********@gm ail.com wrote:
<snip>
>It seems like in the browser scripting world there are always
suprises due to bugs. I don't know about all the bugs.

That does not men that most feature detection tests can be
directly deduced from pinning down the question that needs
answering.
<snip>

Should have read:-

"That does not mean that most feature detection tests cannot be
directly
deduced from pinning down the question that needs answering."

Richard.

Sep 5 '06 #6
Richard Cornford wrote:
"That does not mean that most feature detection tests cannot be
directly deduced from pinning down the question that needs answering."
Most, yes.

But it must be acknowledged that feature-detection is not
behavior-detection.

If a specific browser has a known bug or odd behavior associated with a
feature, simply detecting the feature won't let you fix its faulty behavior.
Although you can write (sometimes complex) code to verify the behavior of a
feature, doing such tests for every feature which might potentially have a
bug in some browser somewhere is wasteful. Sometimes the best way to support
browsers with bugs or quirks is to check for a specific browser in the user
agent string and code accordingly, from WITHIN the proper feature-detection
block.

For example,

if (document.featu re && document.featur e.method) {
if (user agent is Browser 1.5.6.7 on Mac) {
// Do something specific
}
else {
document.featur e.method();
}
}

This way, browser detection will never be executed unless the browser passes
all feature detection tests. If the browser is actually some other browser
with a fake user agent string, it may not pass the feature tests and will
never go into the wrong block to begin with. If it does pass the tests, and
is pretending to be the browser with the bug, then surely the user can
expect code to behave as if they are actually using that browser and deal
with the results.

Alternatively, you could re-define the feature to behave like
standards-compliant browsers so that user agent tests are not required
within the core code at all, and feature detection will work correctly.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Sep 5 '06 #7
Matt Kruse wrote:
Richard Cornford wrote:
>"That does not mean that most feature detection tests cannot be
directly deduced from pinning down the question that needs answering."

Most, yes.

But it must be acknowledged that feature-detection is not
behavior-detection.
Behaviour can be a feature and can often be detected (or worked
around/sidestepped when it cannot).
If a specific browser has a known bug or odd behavior associated with a
feature, simply detecting the feature won't let you fix its faulty behavior.
Although you can write (sometimes complex) code to verify the behavior of a
feature, doing such tests for every feature which might potentially have a
bug in some browser somewhere is wasteful.
How does a known bug in a specific browser become "doing such tests for
every feature which might potentially have a bug in some browser
somewhere"?
Sometimes the best way to support browsers with bugs or
quirks is to check for a specific browser in the user
agent string
No, always the worst thing to do is to start being interested in the
user Agent string. It is not specified as a source of information so
there is no technical foundation for using it as one, and it has
historically been used to mislead software attempting to identify web
browsers so it can be expected to be insufficient for the task.

In all cases where feature detection cannot be applied (and there are
many fewer of those than some people seem to think) object inference
based on a set of features is most likely to give the best indication
on the type of browser.
and code accordingly, from WITHIN the proper feature-detection
block.

For example,

if (document.featu re && document.featur e.method) {
if (user agent is Browser 1.5.6.7 on Mac) {
You can only say "user agent is Browser 1.5.6.7 on Mac" if you can
actually determine that, which you cannot. the best you can say from a
UA string test is that the UA string conforms (in some way) with the
normal/default/expected UA string of "Browser 1.5.6.7 on Mac".
// Do something specific
}
else {
document.featur e.method();
}
}

This way, browser detection will never be executed unless the browser
passes all feature detection tests. If the browser is actually some other
browser with a fake user agent string, it may not pass the feature
tests and will never go into the wrong block to begin with. If it does
pass the tests, and is pretending to be the browser with the bug, then
surely the user can expect code to behave as if they are actually
using that browser and deal with the results.
<snip>

So once again you are blaming the user because a strategy with no
technical foundation results in code that only 'mostly works'.

Richard.

Sep 5 '06 #8
Richard Cornford wrote:
[snip]
In all cases where feature detection cannot be applied (and there are
many fewer of those than some people seem to think) object inference
based on a set of features is most likely to give the best indication
on the type of browser.
I disagree. Object inference might return the same results between version
1.5 and 1.51 of a browser, but version 1.5 might have a very specific bug in
a feature. If you rely on that feature, and the bug can be "fixed" by code,
then you can use browser sniffing but not object inference. In fact, object
inference fails as time goes on - for example, more browsers supporting
document.all and breaking old scripts which assumed things based on
document.all existing.
You can only say "user agent is Browser 1.5.6.7 on Mac" if you can
actually determine that, which you cannot. the best you can say from a
UA string test is that the UA string conforms (in some way) with the
normal/default/expected UA string of "Browser 1.5.6.7 on Mac".
Within that code, what you're saying is "all my object detection code has
determined that the browser behaves consistently with what I expect of
Browser 1.5.6.7 on Mac, and it's telling me that that's what is is." So
trust it!

Instead, you're suggesting we ignore what it's teling us and the
feature-detection evidence that supports it, and instead look at unrelated
features and guess at whether or not the feature we want is broken. That
seems much more subject to failure.

If a browser has document.all, and supports ActiveX, and claims to be IE6,
then that's a pretty convincing argument. Otherwise, it's someone who is
really trying to mess with things on purpose, and if that's the case then
they probably expect problems. Typical users will probably never have these
kidns of issues.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Sep 5 '06 #9
Matt Kruse wrote:
Richard Cornford wrote:
>[snip]
In all cases where feature detection cannot be applied (and there are
many fewer of those than some people seem to think) object inference
based on a set of features is most likely to give the best indication
on the type of browser.

I disagree. Object inference might return the same results between
version 1.5 and 1.51 of a browser, but version 1.5 might have a very
specific bug in a feature.
But UA strings are nether required to include browser version
information nor are they observed to be accurate in the 'version
information' they do provide".
If you rely on that feature, and the bug can be "fixed" by code,
then you can use browser sniffing but not object inference. In fact, object
inference fails as time goes on - for example, more browsers supporting
document.all and breaking old scripts which assumed things based on
document.all existing.
I was not thinking of half-witted object inference, which would be as
bad as UA string based browser detection. There are almost no single
object from which it would be possible to infer that a browser was a
particular type, but finding a browser that had a large set of features
believed to be unique to a particular browser, and did not have
features common to other browsers but known not to be supported on the
browser in question would be a better indicator that the test subject
was that browser than making deductions from what is no more than a
user-configurable sequence of characters. And in many cases precise
version of browsers can be inferred from a sufficient set of object
tests, as most actual browser development represents a sequence of
additions.
>You can only say "user agent is Browser 1.5.6.7 on Mac" if you can
actually determine that, which you cannot. the best you can say from a
UA string test is that the UA string conforms (in some way) with the
normal/default/expected UA string of "Browser 1.5.6.7 on Mac".

Within that code, what you're saying is "all my object detection code has
determined that the browser behaves consistently with what I expect of
Browser 1.5.6.7 on Mac, and it's telling me that that's what is is." So
trust it!
Was that supposed to mean something?
Instead, you're suggesting we ignore what it's teling us and the
feature-detection evidence that supports it, and instead look at unrelated
features and guess at whether or not the feature we want is broken. That
seems much more subject to failure.

If a browser has document.all, and supports ActiveX, and claims to
be IE6, then that's a pretty convincing argument.
And if a browser has _all_ off; global - ActiveXObject -, - attachEvent
-, - CollectGarbage -, - createPopup -, - ScriptEngine -, -
showModalDialog - and - showModelessDia log - properties that are of
fucntion type, - external -, - onafterprint - and - clientInformati on'
properties of object type, activeElement -, - createStyleShee t -, -
execCommand -, - expando -, - fireEvent -, - onbeforedeactiv ate -, -
onbeforeeditfoc us -, - ondatasetchange d -, - recalc -, - Script - and -
uniqueID - proprties on its document, and - addBehavior -, -
behaviorUrns -, - children -, - currentStyle -, - filters -, -
isContentEditab le -, - oncellchange -, - onerrorupdate -, -
parentElement -, - removeBehavior -, - runtimeStyle - and -
setExpression - prperties on its elements, is reading a UA string that
says "Mozilla/4.0 (compatible; MSIE 6.0; Windows 98)" really a more
reliable indicator that the browser is Windows IE 6?
Otherwise, it's someone who is really trying to mess with things
on purpose, and if that's the case then they probably expect problems.
Users are allowed to "mess" with their User Agent strings. If they were
not allowed to then it would not be possible, when it certainly is not
only possible but a direct part of the browser's configuration in some
cases. As the User Agent header/string is not specified as a source of
information they should be no harm in the user changing it (as no
modification could make it mean less then the nothing it is specified
as meaning).
Typical users will probably never have these kidns of issues.
Ah, so user agent string based browser detection 'mostly works', and if
it does not then it's the user's fault?

Richard.

Sep 5 '06 #10

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

Similar topics

23
6541
by: Jeff Rodriguez | last post by:
Here's what I want do: Have a main daemon which starts up several threads in a Boss-Queue structure. From those threads, I want them all to sit and watch a queue. Once an entry goes into the queue, grab it and run a system command. Now I want to make sure that system command doesn't hang forever, so I need some way to kill the command and have the worker thread go back to work waiting for another queue entry.
5
6219
by: Sean Kirkpatrick | last post by:
I'm sure you'll look at this question and wonder if I've been sniffing glue. I haven't, really. My legacy app is written in VB6 and we're chipping away at converting it to .Net. I badly need to be able to step from VB6 IDE into a running VB.Net session to debug the .Net component. This is trivial if I'm running a compiled VB6 app, but that doesn't get me what I need. Can I cross the IDE boundary like this?
3
3692
by: willemp | last post by:
i have the need for a bit of an complex cross-tab report. the following information needs to be shown in 1 report. projectname department hours quarter 1,2,3 and 4 and offcourse all sort of summerized fields. i want all the projectnames listed and then for every department it has
4
1603
by: oopaevah | last post by:
What are the pitfalls of passing a token in the url once a user is logged on so I can remember who they are? I can easily implement this by adding &token=abcdefghijklmnop123 to each internal link on my web pages once the user is logged on. I won't be passing the username or password in the url, just a token that is created when a user logs on. When the server receives the token it maps it back to the account id. This saves the user...
6
8632
by: Simon | last post by:
Hi All, An experiment i'm doing requires requires a synchronous cross-domain request, without using a proxy. I wondered if anyone had any ideas to help me achieve this. Below is what I have tried, including my conclusions/assumptions (which i'll happily be corrected on if it solves my problem!): The requirement not to use a proxy means I can't use the synchronous
1
2563
by: Otacon22 | last post by:
Hi all, I want to create a robot with a router board based on processor atheros 2.6, called "fonera". I have installed a version of linux, Openwrt and python and i want to use it for some reasons, but i have problems to have access to GPIO pins on the board to read and write on harware(pic, memories...) so i want to include into python a porting of io.h I founded an already python wrapped version of io.h called ioport.c that i found...
0
1104
by: Peter Duniho | last post by:
On Mon, 02 Jun 2008 17:14:31 -0700, NvrBst <nvrbst@gmail.comwrote: The general rule is that if it's not one of the five members listed in the docs (under Control.Invoke and other pages), you must use Control.Invoke() or Control.BeginInvoke() to access _any_ member of a Control instance. Now, as you've found, some members not in that list can be successfully used without the cross-thread exception being thrown. One that I am...
6
3987
by: ampo | last post by:
Hello. Can anyone help with cross-domain problem? I have HTML page from server1 that send xmlHTTPRequest to server2. How can I do it? Thanks.
2
5961
by: bhupendrakumar | last post by:
error message System.InvalidOperationException was unhandled Message="Cross-thread operation not valid: Control 'listView2' accessed from a thread other than the thread it was created on." Source="System.Windows.Forms" StackTrace: at System.Windows.Forms.Control.get_Handle() at System.Windows.Forms.Control.SendMessage(Int32 msg, Int32 wparam, Int32 lparam) at...
0
8888
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
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9113
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...
1
6702
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
6011
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
4519
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
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
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
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.