Can anybody recommend a good way to detect screen width on a HTML5 Android App built in Phonegap?
Our page is reasonably fluid – but I need to switch logo’s/footer graphics for smaller screens – so I inject a stylesheet for smaller versions – seems to work fine in general, some phones (HTC Wildfire)seem to think they are 320px width when they are only 240 so I’m struggling!
This is my current detection code – any suggestions would be really helpful!
Cheers
Paul
$(document).ready(function() {
var pageWidth = $(window).width();
var sizeSelected = "chilliAppBeta";
alert(pageWidth);adjustCSS();
function adjustCSS(){
if (pageWidth >= 0) { if(pageWidth < 300) {alert("smallscreen"); applyChilliCSS();} }
if (pageWidth >= 300) { sizeSelected = "chilliAppBeta" }
}
function applyChilliCSS() {
var chilliStyleSheet =$('<link href="CSS/chilliAppBetaSmall.css" rel="stylesheet" class="chilliLink" type="text/css"/>');
$('head').append(chilliStyleSheet);
}
});
You don’t need JavaScript to add additional rules based on your screen dimensions, you can use CSS3 Media Queries instead like:
See this link for specifications and this fiddle (resize the result) for a working demo.