I’m using jQuery’s AJAX for getting new content from server. Data is loaded in JSON:
$.ajax({
url: url,
data: {
'ajax': '1',
},
dataType: 'json',
success: somefunction
});
For server-side application limitation, I’m not able to setup more JSON variables inside so I have to load everything into content. That is why I have to load result into jQuery, than search and replace some elements on page, like this (used in somefunction
):
var somefunction = function(data) {
var con = $('<div></div>').html(data.content); // just $(data.content) is not working
$('div#mainContent').html(con.find('div#ajax-content').html());
... // same process with three more divs
}
EDIT: Please, note that I have to do same process to replace three divs!
There is more about that, but as example, it’s enough I hope. My question: For some logic way, I expect that loading result into DOM ($(data.content)
), parsing to html (con.find('dix#ajax-content').html()
) and back to DOM ($('div#mainContent').html()
) seems to me like loosing some resources and decreasing the perfomance so I would like to know if there is any faster way to do it and load DOM directly, like:
$('div#mainContent').dom(con.find('div#ajax-content').dom());
I tried to google it but maybe I don’t know what to type in. Also jQuery documentation does not helped me a lot.
Some facts:
- jQuery 1.9.1
- jQuery UI 1.10.3 available
Finally, I know that it would be much more better to do something with server-side app to provide more JSON variables, however, I need to write not-so-easy peace of code which is requiring longer time to develop which I don’t have right now. Doing it on client side would be temporary solution for now, however, I don’t want to decrease performace a lot.
Side-question:
is it correct to use find()
function in this case or there is any better one?
EDIT 2 (not working parsing string)
I’m expecting this working but it’s not:
content = '<div id="ajax-title">Pečivo běžné, sladké, slané</div>
<div id="ajax-whereami"><a href="/category/4">Chléba a pečivo</a> » Pečivo běžné, sladké, slané</div>';
$(content);