• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 710
  • Last Modified:

javascript how to print out device width

I use the following css3 media query to apply a style when the device width is 768px. I want to write a html page with javascript that prints out the device-width. They i could run this device to find out the device width instead of hunting for it on google. Any ideas what that javascript or jquery would look like. Note jqueyr $(screen).width() is the browser and not the device width.

@media screen and (device-width: 768px) {
  html{
            background-color: pink;
      }
}
0
glenn_r
Asked:
glenn_r
  • 4
  • 2
2 Solutions
 
Dave BaldwinFixer of ProblemsCommented:
Here you go: http://www.w3schools.com/js/js_window_screen.asp  They have example code linked at the bottom of the page that shows all the screen properties on one page.
0
 
Dave BaldwinFixer of ProblemsCommented:
I'd suggest that you use something like 770px instead of 768px.  I had trouble using the exact width and height.
0
 
Dave BaldwinFixer of ProblemsCommented:
This is from the CSS for one page I've worked on.  Screen sizes 480 and lower are in aother section.
/* Tablet Layout: 481px to 768px. Inherits styles from: Mobile Layout. */

@media only screen and (min-width: 481px) {

Open in new window

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
glenn_rAuthor Commented:
none of those examples get me the device width
i'm not looking for the browser or viewport width
i want to identify the devices width
for example an ipad1 device size is 1024x768
0
 
Dave BaldwinFixer of ProblemsCommented:
I don't understand how the 'device width' is different from the 'screen width'.  In any case, the 'screen width' is all you have to work with.  Did you look at the demo page where it shows all the info that is available?  http://www.w3schools.com/js/tryit.asp?filename=tryjs_screen_all
0
 
quizwedgeCommented:
Device width is typically handled by CSS, but I did find some Javascript code that claims to detect actual device width at http://makandracards.com/makandra/13743-detect-effective-horizontal-pixel-width-on-a-mobile-device-with-javascript

function effectiveDeviceWidth() {
  var deviceWidth = window.orientation == 0 ? window.screen.width : window.screen.height;
  // iOS returns available pixels, Android returns pixels / pixel ratio
  // http://www.quirksmode.org/blog/archives/2012/07/more_about_devi.html
  if (navigator.userAgent.indexOf('Android') >= 0 && window.devicePixelRatio) {
    deviceWidth = deviceWidth / window.devicePixelRatio;
  }
  return deviceWidth;
}

Open in new window

0
 
glenn_rAuthor Commented:
device-width

Here's what i want to do. I want to write javascript to produce the width and device-width values that media queries is using. I have a few devices where I don't know the width and device width and I want to use if for testing.

Media Query Example

@media screen and (max-width: 1024px) {

}

@media (max-device-width: 1024px) {

}

Javascript Example

var width_ = SomeFunctionOrVariableToGetWidth
var devicewidth_ = SomeFunctionOrVariableToGetDeviceWidth
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now