/    Sign up×
Community /Pin to ProfileBookmark

Detecting css value support

How can check with javascript if a value of a property is supported, for instance “fixed” for “position”??

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@rnd_meJul 27.2008 — this returns false if the value is invalid, and the value is it's ok.

[CODE]


function propOK(name, value) {
var t = document.createElement("span");
try {
t.style[name] = value;
} catch (rr) {
t.setAttribute("style", name + ":" + value);
}
if (!!t.style[name]) {
return t.style[name];
}
return false;
}


//tests:
propOK("position","abslute")//false
propOK("position","absolute")//absolute
propOK("color","blue")//blue
propOK("color","blu")//false
propOK("color","#fff")//rgb(255, 255, 255)
[/CODE]
Copy linkTweet thisAlerts:
@mrhooJul 28.2008 — IE6 [B]allows [/B]element.style.position='fixed' (or #element{position:fixed});

but it displays it as static content.

Sometimes you just have to know things.
Copy linkTweet thisAlerts:
@HoboScriptJul 28.2008 — this returns false if the value is invalid, and the value is it's ok.

[CODE]
t.setAttribute("style", name + ":" + value);
[/CODE]


[/QUOTE]


Wouldn't this override all styles?
Copy linkTweet thisAlerts:
@rnd_meJul 28.2008 — Wouldn't this override all styles?[/QUOTE]

not sure what you mean, its a temporary element...


-------------
-op-

don't forget, just because it's valid doesn't mean its useful.

i don't have any code for that...
Copy linkTweet thisAlerts:
@FrankTheTankJul 28.2008 — Wouldn't this override all styles?[/QUOTE]

What I think [B]HoboScript[/B] is asking is this: If you set the one style attribute of the t element using this method, wouldn't all other style attributes of that element be overwritten? I realize that it's a temporary element created only for the purposes of the test. However, I think it's still a legitimate question.

Frank
Copy linkTweet thisAlerts:
@rnd_meJul 28.2008 — What I think [B]HoboScript[/B] is asking is this: If you set the one style attribute of the t element using this method, wouldn't all other style attributes of that element be overwritten? I realize that it's a temporary element created only for the purposes of the test. However, I think it's still a legitimate question.

Frank[/QUOTE]


there are no other styles. period. even if there were, which there aren't, it wouldn't affect the function's operation: to test if a particular rule is parsed by the browser or discarded.
×

Success!

Help @Tabo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...