Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does $($) crash my page?

Disclaimer: Do not try this at home


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

like image 420
Naftali Avatar asked Mar 06 '12 14:03

Naftali


People also ask

Why does a page keep crashing?

There are a few different ways of how a website can crash, including code error, plugin problems, and expired domain, among others. A website is the window of the business. It's how a company communicates with clients. So every second the site is not operational, the business is experiencing missed opportunities.

How do I open a crashed website?

To use the Wayback Machine, head to the Wayback Machine page. Plug the full address of the web page you want to view into the box and click “Browse History”. For example, you can copy and paste this address from your browser's address bar if a web page doesn't load.

Why do my internet tabs keep crashing?

Check for Malware If disabling add-ons and updating your browser does not correct the problem, your computer may be infected with malware such as spyware or a virus. Malware can cause your browser to crash randomly or when you visit certain websites.


2 Answers

$($) 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:

  1. You call $($).
  2. jQuery adds the function argument ($) to an internal ready list.
  3. Some time later, jQuery sees that the DOM is ready and thinks: "Let's call all functions in the ready list".
  4. The only function in the ready list is $, so it calls $.
  5. jQuery sees it should pass the $ function as the argument to those functions.
  6. It calls $ with $ as argument.
  7. 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).
  8. The $ function is called with $ as the argument.
  9. Everything happens again since step 7 applies.
like image 126
pimvdb Avatar answered Sep 24 '22 14:09

pimvdb


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.

like image 23
Diabolic Avatar answered Sep 21 '22 14:09

Diabolic