Javascript provides a couple of choices to find out the display screen width.
After we say display screen
, we imply the browser window’s width itself.
Possibility 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 screen.width
// or just
display screen.width
We will 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);
Possibility 2 – Get Width utilizing jQuery
$(window).width()
You need to use this as follows:
if ($(window).width() < 960) {
alert('Lower than 960');
} else {
alert('Greater than 960');
}