views:

744

answers:

3

Hello,

How do I check if browser supports position:fixed using jQuery. I assume I have to use $.support I think, but how?

Thank you for your time.

+1  A: 

Well, IE6 doesn't support fixed, so you would just check $.browser.msie and $.browser.version with jQuery. This feature is deprecated as of 1.3 and not really recommended to use, though.

You could go down the conditional comments route:

<!--[if IE 6]>
<script src="myscripttohandleie6.js" type="text/javascript"></script>
<![endif]-->
Paolo Bergantino
Thats deprecated. Plus only IE 6 does not support. IE 7 and IE 8 support fixed.
Alec Smart
That method was removed in jQuery 1.3+.
cletus
IE7 in standards mode ofcourse.
Alec Smart
@Alec: I know, which is why I said "not really recommended." Had a feeling IE7 supported it but wasn't sure. fixed that. @cletus: It's not removed; it's deprecated.
Paolo Bergantino
A: 

You could check if position exists by making a code like this:

<html>
<script type="text/javascript">
test = function() {
if(!!document.getElementById("test").style.position) {
alert('true');
}
else{
alert('false');
}
}
</script>

<body>
<p id="test" onclick="test();" style="position:fixed;">Hi</p>
</body>
</html>

Since position exists in all main browser this will always return true. I imagine there isn't a way to check the possible values of position, so you'll have to check which browser and which version the user are viewing your page as Paolo Bergantino said.

Daniel Moura
+3  A: 

The most reliable way would be to actually feature-test it. Browser sniffing is fragile and unreliable.

I have an example of such test in CFT http://yura.thinkweb2.com/cft/#IS_POSITION_FIXED_SUPPORTED. Note that the test should be run after document.body is loaded.

kangax
Great example, and great site, btw!
nickf
Well, thank you :)
kangax
unfortunately this test returns a false positive in MobileSafari, where position:fixed is not supported
prendio2
I have the same problem as Alec, but as prendio2 notes, this test is unfortunately not correct for MobileSafari. Though great otherwise!
hbruce
Hi! I think your feature tests are great. What are your terms for usage of your stuff? :) I'd gladly attribute it to you, if that's all it takes. :D Thanks!
Nikki Erwin Ramirez
Nikki, attribution is fine. Feel free to use it for whatever you need it.
kangax