Side navigation
#7447 closed bug (duplicate)
Opened November 09, 2010 01:00PM UTC
Closed November 09, 2010 01:23PM UTC
Last modified March 13, 2012 06:42PM UTC
wrap block with jquery $(function(){}) causes double execution
Reported by: | Demo_S@ua.fm | Owned by: | |
---|---|---|---|
Priority: | undecided | Milestone: | 1.5 |
Component: | unfiled | Version: | 1.4.3 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
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)
Attachments (0)
Change History (5)
Changed November 09, 2010 01:04PM UTC by comment:1
Changed November 09, 2010 01:23PM UTC by comment:2
resolution: | → duplicate |
---|---|
status: | new → closed |
Changed November 09, 2010 02:31PM UTC by comment:4
confirmed!
<div class="a"> <script> $(function() { $('.a').wrapInner('<div>'); alert($);// will alert two time }) </script> </div>
Changed November 10, 2010 11:34AM UTC by comment:5
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.
http://jsfiddle.net/BRDf7/