473,756 Members | 3,211 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
16 2359
Richard Cornford wrote:
>
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.
I asked about this on the Yahoo! UI group as well. The author of the
code seems very friendly and responded that it was a bit of defensive
coding because of the audience they are targeting with their YUI
scripts. Interesting reasoning and I imagine writing library scripts
for widespread use is quite challenging: all the browser
incompatibiliti es and bugs and all the programmer incompatibiliti es and
bugs too.

<URL: http://groups.yahoo.co m/group/ydn-javascript/message/4585>

So it looks like I can safely drop the "window.ActiveX Object &&" part
of the conditional.

- Peter

Sep 5 '06 #11
VK
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.
That is close related to your other question about Safari 1.x feature
test.
The "pure feature detection" is only possible for the ideal situations
where either the feature is presented and it works as documented across
all UA's with such feature - or the feature is not presented which is
easy to detect by say
if (typeof someObject.some MethodOrPropert y == 'undefined')
or similar.

But as you are forced to act in the imperfect real word, the above
situation will be a rare exception rather than a rule.
Most of the time you have the twilight situation then the feature is
"presented" but
1) it is not actually implemented. It is just a loophole to say True to
trick possible feature tests. Many wannabes (especially older ones) are
bad on it.
or
2) it is implemented but the implementation is broken and requires
extra procedure to compensate the failure.
or
3) it is implemented but in the way UA's producers decided to go, not
in the standard way.

Say Safari 1.x generously covers the situations 2) and 3) and this
"coverage" differs dramatically from one minor version to another. So
say to really deal with Safari 1.x, a simple browsing sniffing is not
enough - you also have to sniff the version.

In the relevance to the opacity and IE: the ActiveXObject feature test
is not relevant to the opacity as IE's filters are not activated over
new ActiveXObject() .
It must be a general combo test to branch onto IE-specific procedures
and workarounds.

Sep 6 '06 #12
pe**********@gm ail.com wrote:
Richard Cornford wrote:
>>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.

I asked about this on the Yahoo! UI group as well. The author of
the code seems very friendly and responded that it was a bit of
defensive coding because of the audience they are targeting with
their YUI scripts.
As I said, it is voodoo. Of the browsers that I know have a global -
ActiveXObject - constructor only Winnows IE 5+ actually supports
filters. Preceding a test for the - filter - string on the - style -
object with a test that will not even pin the browser down to a
Microsoft product is just pointless.
Interesting reasoning
Hardly reasoning at all. You notice the author cannot be specific about
which browsers have which issues. It is just an excuse for doing
something that had no basis in reason in the first palce.
and I imagine writing library scripts
for widespread use is quite challenging: all the browser
incompatibiliti es and bugs and all the programmer
incompatibiliti es and bugs too.
Particularly challenging without a good grasp of javascript or a broad
experience of actual web browsers (both of which are evident in the
faults in the code).
<URL: http://groups.yahoo.co m/group/ydn-javascript/message/4585>

So it looks like I can safely drop the "window.ActiveX Object &&"
part of the conditional.
It should never have been included in the first place; it was pure
voodoo.

Richard.
Sep 6 '06 #13
Richard Cornford wrote:
pe**********@gm ail.com wrote:
Richard Cornford wrote:
>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.
I asked about this on the Yahoo! UI group as well. The author of
the code seems very friendly and responded that it was a bit of
defensive coding because of the audience they are targeting with
their YUI scripts.

As I said, it is voodoo. Of the browsers that I know have a global -
ActiveXObject - constructor only Winnows IE 5+ actually supports
filters. Preceding a test for the - filter - string on the - style -
object with a test that will not even pin the browser down to a
Microsoft product is just pointless.
Interesting reasoning

Hardly reasoning at all. You notice the author cannot be specific about
which browsers have which issues. It is just an excuse for doing
something that had no basis in reason in the first palce.
I think the reasoning makes sense for the Yahoo! UI audience. The
Yahoo! UI is integrated with legacy scripts which may assign directly
to style.filter without first checking it's existance. So for the
browsers that don't have ActiveXObject the YUI setOpacity() function
will still work. The check for ActiveXObject rescues the functionality
of setOpacity() in at least some of the browsers.

I suppose you could argue that they should let the functionality break
in those cases as an indicator that the legacy JavaScript should be
modified. I guess they didn't want to do that.

Peter

Sep 6 '06 #14
pe**********@gm ail.com wrote:
Richard Cornford wrote:
>pe**********@gm ail.com wrote:
<snip>
>>Interesting reasoning

Hardly reasoning at all. You notice the author cannot
be specific about which browsers have which issues. It
is just an excuse for doing something that had no basis
in reason in the first palce.

I think the reasoning makes sense for the Yahoo! UI audience.
The Yahoo! UI is integrated with legacy scripts which may
assign directly to style.filter without first checking it's
existance. So for the browsers that don't have ActiveXObject
the YUI setOpacity() function will still work. The check for
ActiveXObject rescues the functionality of setOpacity() in
at least some of the browsers.
That sounds like someone casting around for any excuse for doing
something when they have realised that they should not have been doing
it in the first place. But even if the true reasoning behind the
'decision' it is a poor approach.

It would be enough to observe that in browser supporting -
style.filter - the TITLE element is accessible, has a - style - object
and that object has a testable - filter - property that is a string. It
would be insane for anyone to directly set a filter on an element that
has no visible manifestation in a page so testing that property may be
the test closet to the issue where the possibility of other scripts
assigning to - style.filter - exists.

It is the mindset that thinks resorting to hit and miss browser sniffing
is an option that should be considered that keeps people from trying to
pin down their problem to the point where a direct feature test is
obvious.
I suppose you could argue that they should let the
functionality break in those cases as an indicator that
the legacy JavaScript should be modified. I guess they
didn't want to do that.
It might be better argued that if a browser has downloaded one mechanism
for assigning to - filter - it should using only that and not be
downloading two. That is exactly the sort of bloat due to needless
repetition that is an argument against monolithic general purpose
libraries as a concept in javascript.

On the other hand I suppose there is a possibility that a separate
sub-system may have been assigning other filters (say drop-shadows), but
then the Yahoo opacity mechanism would screw that up anyway by wiping
the other filter as it assigned its opacity filter. So the though
process that supposedly considered the behaviour of other scripts was
not actually carried through to an appreciation of all the applicable
possibilities.

Richard.
Sep 7 '06 #15
Hi Richard,

Richard Cornford wrote:
pe**********@gm ail.com wrote:
Richard Cornford wrote:
pe**********@gm ail.com wrote:
<snip>
>Interesting reasoning

Hardly reasoning at all. You notice the author cannot
be specific about which browsers have which issues. It
is just an excuse for doing something that had no basis
in reason in the first palce.
I think the reasoning makes sense for the Yahoo! UI audience.
The Yahoo! UI is integrated with legacy scripts which may
assign directly to style.filter without first checking it's
existance. So for the browsers that don't have ActiveXObject
the YUI setOpacity() function will still work. The check for
ActiveXObject rescues the functionality of setOpacity() in
at least some of the browsers.

That sounds like someone casting around for any excuse for doing
something when they have realised that they should not have been doing
it in the first place. But even if the true reasoning behind the
'decision' it is a poor approach.

It would be enough to observe that in browser supporting -
style.filter - the TITLE element is accessible, has a - style - object
and that object has a testable - filter - property that is a string. It
would be insane for anyone to directly set a filter on an element that
has no visible manifestation in a page so testing that property may be
the test closet to the issue where the possibility of other scripts
assigning to - style.filter - exists.
Great solution. Of course I didn't know about that trick. Where is the
list of all these feature tests?

I doubt they thought of that test but perhaps some documents they are
considering don't even have a title element.
It is the mindset that thinks resorting to hit and miss browser sniffing
is an option that should be considered that keeps people from trying to
pin down their problem to the point where a direct feature test is
obvious.
I suppose you could argue that they should let the
functionality break in those cases as an indicator that
the legacy JavaScript should be modified. I guess they
didn't want to do that.

It might be better argued that if a browser has downloaded one mechanism
for assigning to - filter - it should using only that and not be
downloading two. That is exactly the sort of bloat due to needless
repetition that is an argument against monolithic general purpose
libraries as a concept in javascript.

On the other hand I suppose there is a possibility that a separate
sub-system may have been assigning other filters (say drop-shadows), but
then the Yahoo opacity mechanism would screw that up anyway by wiping
the other filter as it assigned its opacity filter.
I mentioned this to the author and he wants to change this eventually.
They are actually quite interested in the thoughts of others and want
the library to be good. I think they might be developing under time
pressure and not be able to revist the code they want to.

Peter

Sep 7 '06 #16
pe**********@gm ail.com wrote:
Richard Cornford wrote:
<snip>
>It would be enough to observe that in browser supporting -
style.filter - ...

Great solution. Of course I didn't know about that trick.
Where is the list of all these feature tests?
There is (and could never be) a list of feature detection tests. Feature
detection is a strategy with a method; you analyse the problem to pin
down the question that needs to be answered and the test that answers
that question in the most direct way (preferably with a one-to-one
relationship to the issue) will then usually be obvious. So, you want to
know when applying - style.filters - is meaningful so you test for
manifestations of - style.filters -, and you also want to put up other
scripts writing to - style.filters - on arbitrary elements so you test
the feature on an element that nobody (sane) would assign a filter to.
I doubt they thought of that test
So do I, but I think that they failed to think of that test because
instead of learning to do browser scripting well they have decided to
hide behind browser sniffing and 'mostly works'.
but perhaps some documents they are
considering don't even have a title element.
The TITLE element is the only element that is _required_ in a valid HTML
document. And they are error-corrected into existence if omitted on
actual browsers (including IE).

<snip>
>On the other hand I suppose there is a possibility that a
separate sub-system may have been assigning other filters
(say drop-shadows), but then the Yahoo opacity mechanism
would screw that up anyway by wiping the other filter as it
assigned its opacity filter.

I mentioned this to the author and he wants to change this
eventually.
So I was correct in assuming that he had not thought through the issues
he was claiming to be acting to mitigate.
They are actually quite interested in the thoughts of others
and want the library to be good.
Even if the thoughts of others are that the style of library manifest is
inappropriate for browser scripting and so can never be 'good'? I think
Yahoo's libraries are in fact only a self-promotion exercise leveraged
off work they would otherwise have to do for themselves internally, so
the only actual intention is to avoid being subject to criticism for
creating faulty code.

Remember that because yahoo use 100% of their code themselves for them
it is an application specific framework not a general purpose library.
I think they might be developing under time
pressure and not be able to revist the code they want to.
Or it already does what Yahoo want it to do internally so additional
development is only worthwhile when it is to remove obvious subjects for
the criticism that would diminish its potential as a (cheep) promotional
exercise.

Richard.
Sep 7 '06 #17

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
6221
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
3694
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
8635
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
2565
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
1106
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
3990
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
9275
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
10034
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...
0
9872
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
8713
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
7248
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
6534
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
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
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
2666
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.