Bug Tracker

Ticket #7447 (closed bug: duplicate)

Opened 4 years ago

Last modified 3 years ago

wrap block with jquery $(function(){}) causes double execution

Reported by: Demo_S@… Owned by:
Priority: undecided Milestone: 1.5
Component: unfiled Version: 1.4.3
Keywords: Cc:
Blocking: Blocked by:

Description

if you havs <script> block inside some <div>, and this <script> block contains $(function(){}), it executes 2 times , not one, if you use wrap to wrap the container <div> with something.

here is the example  http://dis-studio.com/jq-wrap-bug.htm

you'll get 2 alerts with 'aa', instead of one.

of course, i can try to avoid placing jquery inside divs, which are wrapped, but in such case design of my site becomes a bit uglu. it becomes hard to track, which jquery block concerns which html block (html is build from templates and i want to keep jquery code and html code in one template)

Change History

comment:2 Changed 4 years ago by rwaldron

  • Status changed from new to closed
  • Resolution set to duplicate

comment:3 Changed 4 years ago by rwaldron

Duplicate of #7352.

comment:4 Changed 4 years ago by caii

confirmed!

<div class="a">
<script>
$(function() {
	$('.a').wrapInner('<div>');
	alert($);// will alert two time
})

</script>
</div>

comment:5 Changed 4 years ago by jitter

This isn't a duplicate of #7352 which can easily be verified by checking this reduced  test case which still shows the described behavior and wasn't fixed by the fix for #7352.

But I'm not sure this is even a bug. wrap as most (all?) other jQuery DOM-manipulation-methods internally does the same cleaning/processing of elements before they are inserted into the DOM. And one of the things jQuery does is, it pulls out any script elements about to be inserted and executes them and then removes them from the DOM. Taking my reduced test case

DOM before call to wrap

<div id="container">
    <script>$(function(){alert("ready");})</script>
</div>

DOM after the call to wrap

<div>
    <div id="container"></div>
</div>

If I recall correctly one of the reasons jQuery does this is to avoid "Permission Denied" errors that can occur in Internet Explorer when inserting scripts under certain circumstances.

 test case which shows difference between jQuery and pure js approach.

So maybe the real complaint should be that jQuery re-executes script tags that where already executed by the browser. And additionally the script tags just vanish unlike when doing the wrap operation manually in pure js.

Note: See TracTickets for help on using tickets.