Thursday, May 9, 2024
48
rated 0 times [  50] [ 2]  / answers: 1 / hits: 82171  / 12 Years ago, tue, february 28, 2012, 12:00:00

I need to find the device-width of the mobile device.
Although we can specify the content



<meta name=viewport content=width=device-width; user-scalable=no />


But i need to programmatically obtain the device width to compute some logic in my application. How can I get the device width?


More From » jquery-mobile

 Answers
8

The viewport dimensions can be gathered via the window object:



var viewport = {
width : $(window).width(),
height : $(window).height()
};

//can access dimensions like this:
//viewport.height


Though you won't always get perfect results, different devices behave differently and this gives the viewport dimensions, not the screen dimensions.



Alternatively you could check the width of a data-role=page element to find the device-width (since it's set to 100% of the device-width):



var deviceWidth = 0;
$(window).bind('resize', function () {
deviceWidth = $('[data-role=page]').first().width();
}).trigger('resize');​​​

[#87168] Sunday, February 26, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tayaw

Total Points: 749
Total Questions: 88
Total Answers: 86

Location: Djibouti
Member since Sun, Feb 27, 2022
2 Years ago
tayaw questions
;