I have a piece of code that is using .before() and the code it is moving has inline javascript which .before() re-executes. Is there a way to prevent .before() form re-executing javascript code?
+3
A:
You could take the element that you're .before()ing and strip script tags from it. (untested)
$(newHTML).find('script').remove().end().insertBefore(whereToInsert);
Matchu
2010-08-19 16:38:28
yea, I actually tried that and it does work but this issue is part of the jquery ui and not sure if that would mess anything else up. I may end up doing this.
ryanzec
2010-08-19 16:55:27
I'm almost certain that jQuery UI's methods don't involve inline Javascript, so you should be good to go.
Matchu
2010-08-19 17:58:05
+1
A:
Other than moving the javascript out of the HTML that you're relocating, I suppose you could set a flag to true after the first execution of the code, which it checks when executing.
patrick dw
2010-08-19 16:38:44
A:
Maybe you can detach the script, move the elements around, then reattach (if you still need it). Something like this:
var theScript = $('#foo').find('script').detach();
var everythingElse = $('#foo'); // everything minus the script(s)
$('#baz').before( everythingElse ); // Document reflow happens
everythingElse.append( theScript ); // now add it back in?
Not tested, but maybe worth a shot.
Ken Redler
2010-08-19 17:06:23