Skip to main content

Why does $($) crash my page?



Disclaimer: Do not try this at home








Why, if I am using jQuery, does $($) freeze the page?








Inspired by this Area51 question



Source: Tips4all

Comments

  1. $($) is a shortcut for $(document).ready($). So, it will run the function (when the DOM is ready or directly when this is already the case).

    The function passed to .ready is passed the jQuery function for convenience (especially useful when you're in noConflict mode). So, $($) will call $ with $ as argument - and everything will happen again, which is endless recursion.



    Another explanation:


    You call $($).
    jQuery adds the function argument ($) to an internal ready list.
    Some time later, jQuery sees that the DOM is ready and thinks: "Let's call all functions in the ready list".
    The only function in the ready list is $, so it calls $.
    jQuery sees it should pass the $ function as the argument to those functions.
    It calls $ with $ as argument.
    The $ function sees a function as its argument, but because the DOM is ready, it calls the function directly (there is nothing to wait for).
    The function is called with $ as the argument.
    Everything happens again since step 7 applies.

    ReplyDelete
  2. Now this is what I call "jQueryception."

    You're calling whole jQuery library within jQuery.

    More information;

    When you call "$" (defined as jQuery core function by jQuery library) it initializes the jQuery and tries to call the defined function if it has one. When you actually call "$($);" you'll be calling jQuery inside jQuery and it'll be calling jQuery again and again.

    From jQuery 1.7.1 source code;

    // HANDLE: $(function)
    // Shortcut for document ready
    } else if ( jQuery.isFunction( selector ) ) {
    return rootjQuery.ready( selector );
    }


    And

    rootjQuery = jQuery(document);


    As you can see, when you call $($); it tries to call jQuery with the name of your function and if you call it with jQuery again same thing will happen endlessly as I've explained before.

    ReplyDelete
  3. $ is an alias to the jQuery factory function.

    The jQuery factory function, when passed a function as first param, runs that function at document.ready and passes jQuery as the first parameter to it.

    Thus you end up with a infinite recursion starting when document.ready is reached.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex