• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Page Details / Viewport Width

Viewport Width

Viewport Width

There are two methods to get the viewport width: window.innerWidth and document.documentElement.clientWidth. The former is more accurate. The latter has better browser support.

To get the best of both worlds, try innerWidth first, and fallback to clientWidth if not supported.

var viewportWidth = window.innerWidth || document.documentElement.clientWidth;

Source

https://vanillajstoolkit.com/reference/viewport/viewport-width/

Page Details

Related Snippets:

  • Get the height of the viewport
  • Check if the browser tab of the page is focused
  • Get all URL parameters from a query string
  • Notify when element size is changed

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2023 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us