Skip to main content

Bug Tracker

Side navigation

#6790 closed bug ()

Opened July 14, 2010 11:05AM UTC

Closed November 11, 2010 11:09PM UTC

.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().

Attachments (0)
Change History (4)

Changed July 23, 2010 12:20AM UTC by sidewaysmilk comment:1

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.

Changed July 23, 2010 12:20AM UTC by sidewaysmilk comment:2

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

Changed October 26, 2010 05:27PM UTC by rwaldron comment:3

milestone: 1.4.3
owner: → Alistair
priority: → low
status: newpending

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

Changed November 11, 2010 11:09PM UTC by trac-o-bot comment:4

status: pendingclosed

Automatically closed due to 14 days of inactivity.