Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much does this mess up a browser?

Tags:

jquery

A friend of mine recently shared this with me:

$("*").append($("*")); // watch your browser eat itself

What exactly does this jQuery try to do? Does it result in an infinite loop, or does it just mangle the DOM by iteratively appending every element to every other element? I know that appending an element to a different one essentially moves it there from its current parent. But what would happen when it tries to append an element to itself? Does it simply error out and die, or skip that step and move on?

like image 281
Niet the Dark Absol Avatar asked Mar 23 '26 04:03

Niet the Dark Absol


1 Answers

In both Chrome and FFX (didn't test in IE because I'm scared) I get a Hierarchy Request Error which happens when you try to append a node to itself or append to a null node, or a bunch of other similar reasons.

This means that logically it actually does go through the DOM tree and attempt to append others first .. the selector selects the same node twice and tries to append it to itself, which results in the error.

like image 121
Explosion Pills Avatar answered Mar 25 '26 20:03

Explosion Pills



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!