Skip to content

Instantly share code, notes, and snippets.

@faceleg
Last active December 12, 2015 02:58
Show Gist options
  • Save faceleg/4703441 to your computer and use it in GitHub Desktop.
Save faceleg/4703441 to your computer and use it in GitHub Desktop.
Template for jQuery ajax handler that runs scripts contained in the returned data.
<form action="" method="post">
<input type="text" name="name" />
<button type="submit">Submit</button>
</form>
<div id="result"></div>
<script type="src/javascript">
$.ajax({
type: 'POST',
url: location.href,
data: $(this).serialize()
}).success(function(data) {
wrapper.animate({ opacity: 0 }, function() {
wrapper.html($.parseHTML(data))
.animate({ opacity: 1 });
// Run any scripts included in the returned HTML
$(data).filter('script').each(function() {
$.globalEval(this.text);
});
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment