tysonmatanich / GetViewportWidth

An easy way to get the viewport width using JavaScript.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

If you are using this repo it will no longer be maintained, please instead use viewportSize which also allows you to get the viewport height.

GetViewportWidth

Allows you to get the width of the viewport using JavaScript. Most people rely on window.innerWidth or document.documentElement.clientWidth which do not always accurately report the viewport width used in CSS media queries. For example the viewport width in Chrome changes depending on if scrollbars are visible however window.innerWidth does not. Script uses feature detection instead of relying on user agent detection.

Demo URL: http://tysonmatanich.github.io/GetViewportWidth/

Size and delivery

Currently, getViewportWidth.js compresses to around 541 bytes (~0.5 KB), after minify and gzip. To minify, you might try these online tools: [Microsoft Ajax Minifier]:(http://ajaxmin.codeplex.com/), [Uglify]:(http://marijnhaverbeke.nl/uglifyjs), [Yahoo Compressor]:(http://refresh-sf.com/yui/), or Closure Compiler. Serve with gzip compression.

Sample

<script type="text/javascript">
	var viewportWidth = window.getViewportWidth();
</script>

Support

GetViewportWidth supports a broad range of browsers and devices (there are currently no known unsupported browsers).

About

An easy way to get the viewport width using JavaScript.


Languages

Language:HTML 57.6%Language:JavaScript 42.4%