Created
September 7, 2012 17:32
-
-
Save joshuacronemeyer/3667984 to your computer and use it in GitHub Desktop.
Preprocessing AJAX HTML responses with jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var render = function(responseHTML){ | |
// Turn HTML response into jQuery object. | |
var $responseHTML = $(responseHTML); | |
// Grab thumbnails HTML out of the envelope | |
var $thumbnailVideos = $($responseHTML.find('.thumbnail-videos-envelope').html()); | |
// Grab backgrounds HTML out of the envelope | |
var $backgroundVideos = $($responseHTML.find('.background-videos-envelope').html()); | |
// Now I can update the two different parts of the page from this single HTML response. | |
$('#thumbnail-container').append($thumbnailVideos); | |
$('body').append($backgroundVideos); | |
}; | |
$.ajax({ url: "http_request_url", | |
type: 'get', | |
success: function(data) { | |
render(data); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Outer div and envelopes are just to separate the two distinct HTML snippets, and will be thrown away --> | |
<div> | |
<span class="thumbnail-videos-envelope"> | |
<video id="bg-3"></video> | |
<video id="bg-4"></video> | |
</span> | |
<span class="background-videos-envelope"> | |
<video id="bg-3"></video> | |
<video id="bg-4"></video> | |
</span> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment