Solved

javascript how to print out device width

Posted on 2014-03-20
7
534 Views
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) {
  html{
            background-color: pink;
      }
}
0
Comment
Question by:glenn_r
  • 4
  • 2
7 Comments
 
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.
0
 
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.
0
 
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

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:glenn_r
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
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 98 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
0
 
LVL 14

Assisted Solution

by:quizwedge
quizwedge earned 97 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

0
 

Author Comment

by:glenn_r
ID: 39945278
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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
In this tutorial viewers will learn how to increase their history states in Photoshop To undo more than one history state, use Ctrl + Alt + Z, not just Ctrl + Z: View the History window by going to Window > History: The default number of history sta…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now