{"id":238,"date":"2021-03-21T15:30:48","date_gmt":"2021-03-21T15:30:48","guid":{"rendered":"https:\/\/wshop.fi\/eng\/?p=238"},"modified":"2021-03-21T15:47:47","modified_gmt":"2021-03-21T15:47:47","slug":"jquery-window-width","status":"publish","type":"post","link":"https:\/\/wshop.fi\/eng\/jquery-window-width\/","title":{"rendered":"jQuery Window Width &#8211; Determine Browser Window Size, Responsive Design"},"content":{"rendered":"\n<p>jquery window width, you\u2019ll need it if you\u2019re working with&nbsp;<strong>Responsive Design<\/strong>&nbsp;and\/or need to figure out the&nbsp;<strong>width of the browser Window?<\/strong>&nbsp;Are you using a responsive design layout and using viewport, but need a way to&nbsp;<strong>determine window width?<\/strong>&nbsp;jQuery width() and jQuery resize() can help.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Responsive Design<\/h2>\n\n\n\n<p>With so many devices able to access the internet,&nbsp;<strong>Responsive Design<\/strong>&nbsp;is becoming a necessity. &nbsp; Smart phones, tablets, laptops, and desktop computers have different viewable areas.&nbsp;Viewport is the window size of the device. We\u2019ll use jquery width and resize functions to&nbsp;<strong>determine the window width<\/strong>&nbsp;of the screen thus aiding us in our Responsive Design.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" \/&gt;<\/code><\/pre>\n\n\n\n<p>To determine the browser window width and if it gets resized we\u2019ll use:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>jquery width<\/li><li>jquery resize<\/li><li>window<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">jQuery Window Width Determined<\/h2>\n\n\n\n<p>jQuery .width() returns a numerical value only, i.e.325.&nbsp; The value is in pixels.&nbsp; However, .css(width) will return the number and units, i.e. 325px. &nbsp; We want to use&nbsp;<strong>jQuery window width<\/strong>&nbsp;since we\u2019ll test for numerical values later.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var wi = $(window).width(); \/\/ Stores the numerical value of the width into the variable \"wi\"<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Window vs. Document vs. Screen<\/h2>\n\n\n\n<p>Why window width?&nbsp; The window \u201cholds\u201d the document. Because the document is what gets loaded into the Window object we\u2019ll use $(window).width().&nbsp; I also use&nbsp; jquery window width instead of screen width.&nbsp; If someone doesn\u2019t have their browser&nbsp;<strong>window<\/strong>&nbsp;maximized to full screen then it only makes sense to get the width of the window size they are using and not the screen.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$(window).width();   \/\/ This will return the width of browser viewport\n$(document).width(); \/\/ This will return the width of HTML document\n$(screen).width(); \/\/ This will return the width of the users screen\n \n\/\/Since the document is what gets loaded into the Window object, and the \n\/\/browser window may not be maximized to full screen we'll use $(window).width();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">jQuery Window Resize<\/h2>\n\n\n\n<p>What happens if the window is resized?&nbsp; Luckily jQuery has a function to test if an object gets resized.&nbsp; The code below will show the window width when resized.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$(window).resize(function() {\n        var wi = $(window).width();\n        $(\"p.testp\").text('Screen width is currently: ' + wi + 'px.');\n});<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">jQuery Window Width<\/h2>\n\n\n\n<p>The code below will use jQuery window width to show the initial screen width, but not once it\u2019s resized.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$(window).resize(function() {\n        var wi = $(window).width();\n        $(\"p.testp\").text('Screen width is currently: ' + wi + 'px.');\n});<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">jQuery Window Width<\/h2>\n\n\n\n<p>The code below will use jQuery window width to show the initial screen width, but not once it\u2019s resized.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var wi = $(window).width();  \n$(\"p.testp\").text('Initial screen width is currently: ' + wi + 'px.');<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">jQuery Window Width Determined Initially and When Resized<\/h2>\n\n\n\n<p>We\u2019ll put it all together here.&nbsp; We\u2019ll use jQuery window width and resize as we did above.&nbsp; I will provide the HTML to be manipulated further below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*\n* How to detect browser width\n*\/\n$(window).ready(function() {\n    var wi = $(window).width();  \n    $(\"p.testp\").text('Initial screen width is currently: ' + wi + 'px.');\n \n    $(window).resize(function() {\n        var wi = $(window).width();\n \n        if (wi &lt;= 480){\n            $(\"p.testp\").text('Screen width is less than or equal to 480px. Width is currently: ' + wi + 'px.');\n            }\n        else if (wi &lt;= 767){\n            $(\"p.testp\").text('Screen width is between 481px and 767px. Width is currently: ' + wi + 'px.');\n            }\n        else if (wi &lt;= 980){\n            $(\"p.testp\").text('Screen width is between 768px and 980px. Width is currently: ' + wi + 'px.');\n            }\n        else if (wi &lt;= 1200){\n            $(\"p.testp\").text('Screen width is between 981px and 1199px. Width is currently: ' + wi + 'px.');\n            }\n        else {\n            $(\"p.testp\").text('Screen width is greater than 1200px. Width is currently: ' + wi + 'px.');\n            }\n    });            \n});<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">jQuery Window Width HTML<\/h2>\n\n\n\n<p>Below is the HTML required. We use the jQuery \/ javascript code above to manipulate the HTML code below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Strict\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-strict.dtd\"&gt;\n&lt;html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"&gt;\n&lt;head&gt;\n    &lt;title&gt;Detect Browser Window Width&lt;\/title&gt;\n    &lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=iso-8859-1\" \/&gt;\n    &lt;script type=\"text\/javascript\" src=\"jquery.js\"&gt;&lt;\/script&gt;\n    &lt;!-- Note - You must place your Javascript in an external file for this to work correctly --&gt;\n    &lt;script type=\"text\/javascript\" src=\"detect.js\"&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;p class=\"testp\"&gt;&lt;\/p&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>jquery window width, you\u2019ll need it if you\u2019re working with&nbsp;Responsive Design&nbsp;and\/or need to figure out the&nbsp;width of the browser Window?&nbsp;Are you using a responsive design layout and using viewport, but need a way to&nbsp;determine window width?&nbsp;jQuery width() and jQuery resize() can help. Responsive Design With so many devices able to access the internet,&nbsp;Responsive Design&nbsp;is becoming [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":127,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":{"0":"post-238","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-coding"},"_links":{"self":[{"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/posts\/238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/comments?post=238"}],"version-history":[{"count":2,"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/posts\/238\/revisions"}],"predecessor-version":[{"id":240,"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/posts\/238\/revisions\/240"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/media\/127"}],"wp:attachment":[{"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/media?parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/categories?post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/tags?post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}