Bug Tracker

Opened 9 years ago

Closed 9 years ago

#6790 closed bug

.css() returning inconsistent values between browsers

Reported by: Alistair Owned by: Alistair
Priority: low Milestone:
Component: css Version: 1.4.2
Keywords: Cc:
Blocked by: Blocking:

Description

Using jQuery v1.4.2, Firefox 3.6.6, Chrome 4.1, IE 6,7,8.

Example, I'm using the .css() function to get the value of the css attribute "background". The attribute has *not* previously been assigned a value (either in script or style sheet).

e.g. alert($(this).css("background"));

In Firefox and Chrome, the return value is an empty string (""). However in IE 6,7,8, the return value is "undefined".

Considering jQuery aims to be cross-browser and free us from writing browser-conditional code, I think the desired behaviour would be a consistent return type (ie. string) across all browsers, whether the attribute is set or not.

The result is not needing to check first if a value is present before doing something with it.

The following statement will not work across all browsers, because you can't do indexOf() on "undefined": alert($(this).css("background").indexOf("somepic.gif"));

Inconsistent return values occur for other CSS attributes as well. For example, if "background-image" is not set, .css("background-image") returns "none" in IE, and "" in Firefox & Chrome. There are many other cases as well.

It would be great if jQuery could provide standard rules across all browsers for the values that are returned from .css(), so we don't have to write browser-specific code for them.

If we want the browser-specific value, we can always use $(something).get(0).style.backgroundImage etc.

It would be a matter of jQuery checking for something like "none" for "background-image" and changing it to "" for us. This would help give us consistent return values for .css().

Change History (4)

comment:1 Changed 9 years ago by sidewaysmilk

I have noted that in $('div').css({filter}); returns "" in IE 6, 7 and 8, "none" in iOS, Chrome and Firefox, and null in Android.

comment:2 Changed 9 years ago by sidewaysmilk

Dammit. That's $('div').css('filter');

comment:3 Changed 9 years ago by Rick Waldron

Milestone: 1.4.3
Owner: set to Alistair
Priority: low
Status: newpending

Can you provide a reduced test case on jsFiddle for this issue? Thanks!

comment:4 Changed 9 years ago by trac-o-bot

Status: pendingclosed

Automatically closed due to 14 days of inactivity.

Note: See TracTickets for help on using tickets.