Bug Tracker

Opened 12 years ago

Closed 12 years ago

Last modified 11 years ago

#7447 closed bug (duplicate)

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

Reported by: [email protected] Owned by:
Priority: undecided Milestone: 1.5
Component: unfiled Version: 1.4.3
Keywords: Cc:
Blocked by: Blocking:


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 (5)

comment:2 Changed 12 years ago by Rick Waldron

Resolution: duplicate
Status: newclosed

comment:3 Changed 12 years ago by Rick Waldron

Duplicate of #7352.

comment:4 Changed 12 years ago by caii


<div class="a">
$(function() {
	alert($);// will alert two time


comment:5 Changed 12 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">

DOM after the call to wrap

    <div id="container"></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.