0

Trying to write a function that will test conditions with certain browser window size using JavaScript. For example:

if ( window.width <= 500 && window.height <= 500){
do something ;
}else{
do something;

Are there any existing commands for JavaScript?

MaryK
  • 3
  • 1

3 Answers3

0

You can use window.innerWidth and window.innerHeight. Those should give you the viewport width and height respectively.

isaacsan 123
  • 1,045
  • 8
  • 11
0

In javascript, if you are running in a browser, you can use window.innerWidth and window.innerHeight, or also window.clientWidth and window.clientHeight.

There are little differences, you can read more about it here

Drago96
  • 1,265
  • 10
  • 19
0

Use window.screen.availHeight window.screen.availWidth and set them in variables to work with for example..

const height = window.screen.availHeight
const width window.screen.availWidth

if(width) {
do something}
Koni
  • 452
  • 1
  • 7
  • 19
  • Thank you. Can I set height and weight values to these commands? Or it's just returns value? What I'm trying to do is to write a script/condition to click on an element on a page. Element is visible when browser page in a small viewport. Like if width and height is "value", then click on element. – MaryK Jun 02 '21 at 17:48