Reputation: 4082
I am developing an application in phonegap android.
I am trying to get the device width on device ready. I tried this code.
First time when application starts its giving (480, I am using samsung i9003 ) width ( alert ) and when I hit the backbutton and again running application its showing 320.
I has been stucked here .. please help
<!DOCTYPE HTML>
<html>
<head>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<meta name="viewport" content="width=device-width; user-scalable=no" />
<link rel="stylesheet" href="css/jquery.mobile-1.1.1.css" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>Title</title>
</head>
<body onload="onLoad()">
<div class="logo"></div>
<div class="content">
<div id="justt"></div>
</div>
<script type="text/javascript" src="cordova-2.2.0.js"></script>
<script src="js/jquery.js"></script>
<script type="text/javascript" charset="utf-8">
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
var width=0;
width=$(window).width()
$('#justt').css('width',(width)+'px');
alert(width);
}
</script>
</body>
</html>
Upvotes: 0
Views: 2384
Reputation: 57309
Why don't you try to get a page content width:
var screenWidth = 0;
$(window).bind('resize', function () {
screenWidth = $('[data-role="page"]').first().width());
}).trigger('resize');
This will give you page content width, even if page is resized.
You won't always get perfect results while getting viewport screen dimensions ($(window).width()), different devices behave differently.
$(window).width() and $(window).height() will give you the viewport dimensions, not the screen dimensions. Here you will find detailed overview of this problem.
Upvotes: 1