How to get the browser viewport dimensions in JavaScript

2 Answers

0 votes
const width = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0)
const height = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0)

console.log(width);
console.log(height);
  
    
    
/*
run:

1913
500
    
*/

 



answered Jan 30, 2021 by avibootz
0 votes
let element = (document.compatMode === "CSS1Compat") ? document.documentElement : document.body;

let width = element.clientWidth;
let height = element.clientHeight;


console.log(width);
console.log(height);
  
    
    
/*
run:

1913
500
    
*/

 



answered Jan 30, 2021 by avibootz
...