Skip to content

Latest commit

 

History

History
41 lines (37 loc) · 855 Bytes

README.md

File metadata and controls

41 lines (37 loc) · 855 Bytes

Get Window Client Dimentions

Paste the below JavaScript:

const isSupported = () => typeof window !== 'undefined'
const getWindowWidth = () => {
  if (!isSupported()) {
    return 0
  }
  return (
    document.getElementsByTagName('body')[0].clientWidth
  )
}

const isSupported = () => typeof window !== 'undefined'
const getActualWidth = () => {
  if (!isSupported()) {
    return 0
  }
  return (
    window.innerWidth
  )
}

in your .JS file

window.addEventListener('scroll', runScript)
window.addEventListener('resize', runScript)
window.addEventListener('load', runScript)

function runScript() {
  console.log((getWindowWidth() + 'px')
  console.log((getActualWidth() + 'px')
}

See it in action: https://jsfiddle.net/timhjellum/ze2wvax8/.