javascript how to print out device width

Posted on 2014-03-20
Medium Priority
Last Modified: 2014-04-30
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) {
            background-color: pink;
Question by:glenn_r
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39943966
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.
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39943968
I'd suggest that you use something like 770px instead of 768px.  I had trouble using the exact width and height.
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39943977
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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 39944013
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
LVL 83

Accepted Solution

Dave Baldwin earned 392 total points
ID: 39944168
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
LVL 14

Assisted Solution

quizwedge earned 388 total points
ID: 39944576
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


Author Comment

ID: 39945278

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

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question