سؤال

I'm having problems with $(document).ready function. The problem is that it is not executed and there are no errors. I have tried to change $ to jQuery, but the result is the same. I'm sure that jQuery is loaded because other JQ functions work.

Here is my code:

(function($){
    alert( $(window).width() );
    $(document).ready(function(){
        alert(1);
    }); 
})(jQuery);

The first alert is working fine but the second is not.

هل كانت مفيدة؟

المحلول

What you are looking for is Differences between document.ready and $function

They both are same

(function($){

})(jQuery);

or

$(document).ready(function(){

});

although the code you have provided should work but what you need to do is either use

(function($){
    alert( $(window).width() );
    alert(1); 
})(jQuery); 

or

$(document).ready(function(){
    alert( $(window).width() );
    alert(1);
}); 

Update

Also take a look at $.ready documentation http://api.jquery.com/ready/

نصائح أخرى

What show Firebug console? Try to debug with function:

console.log.("It's working!");

Most likely jQuery it wasn't initialized yet.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top