javascript - jQuery.clone() causes browser to hang - Stack Overflow

Why does the following jQuery code cause my browser to hang?Caution: do not run this unless you are pre

Why does the following jQuery code cause my browser to hang?

Caution: do not run this unless you are prepared to force quit your browser

<!DOCTYPE HTML> 
  <title>Simple clone</title> 
  <script type="text/javascript" src="jquery.js"></script> 

  <div>
    <script>$('div').clone().appendTo('body');</script>
  </div>

Edit

For those in the "infinite loop" camp, that should not be an issue. A perfectly safe (non-jQuery) version is:

  <div>div
    <script>
      var el = document.getElementsByTagName('div')[0];
      document.body.appendChild(el.cloneNode(true));
    </script>
  </div>

So the issue is specifically related to how jQuery does clones.

Edit 2

It seems that jQuery causes script elements in clones to be executed. That isn't standard behaviour and is something about how jQuery does clones that is quite unexpected.

Why does the following jQuery code cause my browser to hang?

Caution: do not run this unless you are prepared to force quit your browser

<!DOCTYPE HTML> 
  <title>Simple clone</title> 
  <script type="text/javascript" src="jquery.js"></script> 

  <div>
    <script>$('div').clone().appendTo('body');</script>
  </div>

Edit

For those in the "infinite loop" camp, that should not be an issue. A perfectly safe (non-jQuery) version is:

  <div>div
    <script>
      var el = document.getElementsByTagName('div')[0];
      document.body.appendChild(el.cloneNode(true));
    </script>
  </div>

So the issue is specifically related to how jQuery does clones.

Edit 2

It seems that jQuery causes script elements in clones to be executed. That isn't standard behaviour and is something about how jQuery does clones that is quite unexpected.

Share Improve this question edited Dec 5, 2011 at 1:30 RobG asked Dec 5, 2011 at 0:48 RobGRobG 148k32 gold badges179 silver badges215 bronze badges 15
  • 7 It seems kind-of obvious... – Šime Vidas Commented Dec 5, 2011 at 0:51
  • 3 Your cloning the function itself, creating an infinite loop, destroying the entire intertweetyfaceweb galaxy, distrupting the space-time continuum! – adeneo Commented Dec 5, 2011 at 0:52
  • The interesting question is why the non jQuery version doesn't cause an infinite loop – Raynos Commented Dec 5, 2011 at 1:15
  • 1 aspiringcraftsman./2008/01/03/art-of-separation-of-concerns – Incognito Commented Dec 5, 2011 at 1:30
  • 1 @Raynos — HTML5 isn't a standard, much of it documents existing behaviour without saying that's what it's doing. The relevant standard (DOM 3 Core) doesn't say what should happen. However, given that the default behaviour of cloneNode is to not re-execute script elements (even though it clones them), then jQuery shouldn't do it either, even if the cloning script is inside the cloned element (I don't know why jQuery does it but it certainly isn't expected or designed behaviour). – RobG Commented Dec 5, 2011 at 2:48
 |  Show 10 more ments

2 Answers 2

Reset to default 7

Because you're cloning a div that has inside of it a script that says to clone all divs. The original div's script that says to clone all divs is cloned too, and so on, ad infinitum

Of course you could do:

  <script type="text/javascript" src="jquery.js"></script> 
  <script type="text/javascript">
     $(function() {
        $('div').clone().appendTo('body');
     });
  </script>

  <div>
    Safe Now
  </div>

It's an infinite loop. You're cloning a div which contains a script that clones 2 divs that have a script that clone 4...

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745352175a4623898.html

相关推荐

  • javascript - jQuery.clone() causes browser to hang - Stack Overflow

    Why does the following jQuery code cause my browser to hang?Caution: do not run this unless you are pre

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信