The key to understanding here is what $(function() {}) does. The code inside it is not executed until the document is ready. Entering the code in it is equivalent to putting the code in this:
$(document).ready(function () {
You want to place the resize event inside $(function() {}) , for example:
function checkMq() { if (Modernizr.mq('only screen and (min-width: 1140px)')) { $('div#ss1').html('<div>[snip]</div>'); $('div#ss1').append('<div><gcse:searchbox-only></gcse:searchbox-only></div>'); } if (Modernizr.mq('only screen and (max-width: 1139px)')) { $('div#ss2').html('<div>[snip]</div>'); $('div#ss2').append('<div><gcse:searchbox-only></gcse:searchbox-only></div>'); } } $(function() {
If you only have $(window).resize(function() {}) without using $(function() {}) , this will not work, because the document is not ready to work, and it will not be ready to add listeners events.
source share