Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the Media Query equivilent for Jquery
    primarykey
    data
    text
    <p>When my website is on a desktop-sized display, I want additional jQuery scripts to load that will not be loaded on mobile sized displays. </p> <p>CSS has Media Queries which makes it easy to load CSS files depending on browser size. Is there an equivalent of this for Jquery?</p> <hr/> <hr/> <p><strong>Background Research:</strong></p> <p>Here are some solutions I have found and the drawbacks of each.</p> <p><strong>Solution 1:</strong> <strong><a href="http://wickynilliams.github.com/enquire.js/" rel="nofollow noreferrer">Enquire.js</a></strong> - This is supposed to be the media query equivalent for Javascript (I think). However, the documentation isn't very clear (at least for a beginner). </p> <p>For example, <a href="http://css-tricks.com/enquire-js-media-query-callbacks-in-javascript/" rel="nofollow noreferrer">this tutorial</a> gives the following example:</p> <pre><code>enquire.register("max-width: 960px", function() { // put your code here }); </code></pre> <p>However, if you copy and paste Jquery code where it says, nothing happens! I think you might have to place a link to your Jquery file, but the documentation doesn't explain how to do that!</p> <p><strong>Solution 2:</strong> <strong><a href="http://www.venveo.com/articles/view/quick-tip-jquery-media-queries" rel="nofollow noreferrer">Use Window Resize</a></strong> - You can use <code>$(window).resize</code> to trigger scripts. However, it will only work if the user re-sizes the browser. This is no good for a desktop user whose window is always maximized.</p> <p>A further problem is that the script will be loaded each time the browser is re-sized. This can be a problem for some scripts (see the example <a href="http://jsfiddle.net/QcmpG/1/embedded/result/" rel="nofollow noreferrer">here</a> where the words false/true are repeated endlessly if the window is resized several times). </p> <p><strong>Solution 3:</strong> <strong><a href="https://stackoverflow.com/questions/6461300/triggering-jquery-with-css-media-queries">Set a hidden 100% width DIV and then run a script that checks it's width</a></strong> - The drawback of this is that it only runs when the page first loads, so it's not good for a user who changes the size of their window (or orientation of the device). <a href="http://jsfiddle.net/9X4up/" rel="nofollow noreferrer">You can run a timer so it checks</a> the width every 1 second, but that doesn't seem very efficient.</p> <p>Does anyone know of solutions that works?</p> <p>Thanks.</p> <p>PS. This question is about a Media Query equivalent for Jquery. It is NOT about trying to recreating CSS Media Queries in Javascript (i.e. it is NOT about using Jquery to load different CSS files for different window sizes which is achieved with <a href="http://code.google.com/p/css3-mediaqueries-js/" rel="nofollow noreferrer">css3-mediaqueries-js</a>).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload