Sunday, May 21, 2023
HomeSoftware EngineeringFind out how to get the Display screen Width in Javascript

Find out how to get the Display screen Width in Javascript


Javascript offers a number of choices to find out the display width.

Once we say display, we imply the browser window’s width itself.

Choice 1 – Utilizing Native Javascript

Utilizing native Javascript objects, we will discover the innerWidth and innerHeight:

var w = window.innerWidth;
var h = window.innerHeight;

The window object additionally permits for:

window.display.width
// or just
display.width

We are able to write some code to inform us the width and peak with fallbacks:

var win = window,
    doc = doc,
    docElem = doc.documentElement,
    physique = doc.getElementsByTagName('physique')[0],
    x = win.innerWidth || docElem.clientWidth || physique.clientWidth,
    y = win.innerHeight|| docElem.clientHeight|| physique.clientHeight;
alert(x + ' × ' + y);

Choice 2 – Get Width utilizing jQuery

$(window).width()

You should utilize this as follows:

if ($(window).width() < 960) {
   alert('Lower than 960');
} else {
   alert('Greater than 960');
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments