I'm trying to do something that might be frowned upon, but I have no idea how else to do this. What I'm trying to do is capture the "state" of a specific div with jQuery like so:
var content = $("#Layout").html();
I'm doing this because I want to "save" the div's previous content somewhere, pull some content dynamically into the same div (#Layout
) and then replace the old content back when the user clicks back. Now this all kind of works, but obviously all my jQuery needs to be re-evaluated (for stuff like powerTips and so on).
My initial thinking was to just use some regex to get all the <script>
tags from the content
variable and do something like eval(matched);
, but I don't think this is the best solution.
Does anybody have any ideas as to how this can be done efficiently and easily? If this is the only solution, does anybody have some clue what regex I can use for this? Been searching for about an hour, and found some articles, but nothing seems to be doing the trick.
Here is the full code of what I've been trying thus far.
$(".loadDialog").live('click', function(e){
e.preventDefault();
var url = $(this).attr('data-rel');
$(".AjaxLoading").show();
var Layout = $("#Layout");
previousContent = Layout.html();
Layout.load(url, function(response, status, xhr) {
if (status === "error") {
showErrorDialog();
Layout.html(previousContent);
} else {
//Create a "Back" button
var backButton = '<div class="iconSprite backButton close-dialog" style="position: relative;"><a href="javascript:" class="close-dialog">Back</a></div>';
Layout.prepend(backButton)
}
$(".AjaxLoading").hide();
});
return false;
});
$(".close-dialog").live('click', function(e) {
e.preventDefault();
$("#Layout").html(previousContent);
var scriptMatcher = /<script[\s\S]*?>[\s\S]*?<\/script>/gm;
var match;
while (match = scriptMatcher.exec(previousContent)) {
eval(match);
}
return false;
});
Please don't hate on this, I'm no jQuery expert.
Thanks!