I have followed the answer to this question in order to detect whether a device is iOS, and then modify the CSS if true: Detect if device is iOS
I have this code in the head of my document (a Wordpress header.php template):
<script type="text/javascript">
var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
if (iOS){
$(".noParallax").css("display","block");
$(".myParallax").css("display","none");
}
</script>
The CSS in my style.css sheet is as follows:
.noParallax {
display:none;
}
.myParallax {
background-attachment:fixed;
display:block;
}
The idea is that div.noParalax is displayed when the browser detects ios, but hidden when it's not an iOS device, then div.myParallax displays instead.
However, it doesn't seem to be working. Do I need to link to any special libraries in order for it to work? Or is my code completely wrong (I am not familiar really with javascript or how it works.)
Alternative Test
Frustratingly, I also tried this to no avail:
<script type="text/javascript">
if( /iPhone|iPad|iPod/i.test(navigator.userAgent) ) {
$(".noParallax").css("display","block");
$(".myParallax").css("display","none");
} else {
$(".noParallax").css("display","none");
$(".myParallax").css("display","block");
}
</script>
Which doesn't work either. No CSS styles are applied to the relevant divs. However, the similar script below DOES work:
<script type="text/javascript">
if( /iPhone|iPad|iPod/i.test(navigator.userAgent) ) {
alert("This is iOS!");
} else if( /Android|BlackBerry/i.test(navigator.userAgent) ) {
alert("This is Android or Blackberry!");
} else {
alert("This is something else!");
}
</script>
So what is it that's wrong with the first version of this code that specifically modifies CSS. Is my syntax wrong?
Thank you in advance for any help anyone can offer.