I’m trying to make jquery-mobile + phonegap app which is just simplified version of some sites. Early approach was deadly simple. Just build some model and collections for that site and override parse() to fill them. Here goes some code for it.
var Posts = Backbone.Collection.extend({
model: Post,
parse: function(response){
var posts = [];
var $response = $(response);
var $tr = $response.find('div.board_main tr');
$tr.each(function(index){
if(index<2){ return; } // skip notice post
var $post = $(this);
var post_id = $post.find('td').eq(0).text();
var $post_subject = $post.find('td.post_subject');
var post_subject;
if( $post_subject.find('a').length ){
post_subject = $post_subject.find('a').text();
}else{
return;
}
posts.push({
id: post_id,
subject: post_subject
});
});
return posts;
}
});
Yes I’m using jQuery to parse HTML to make model so wrap response html with $, but at that time, browser will download assets like img, scripts in that html even I didn’t append it to somewhere in body.
How can I cancel this behavior? Or should I choose another approach to parse html?
==
EDIT: I chose preprocessing with avascript regexp before wrapping with jQuery. (Remove iframe/script tags and replace img.src was ok to go ahead) I know this is dirty but now safari won’t download assets.. happy now.. 🙂
function removeTag(html, tag){
var re = new RegExp('<'+tag + '(.+?)' + '</'+tag+'>', 'g');
return html.replace(re,'');
}
function removeImgSrc(html){
return html.replace(/<img([^>]*)\ssrc=/gi,'<img$1 data-src=');
}
See my own related question: Parsing invalid HTML with jQuery, without adding to DOM?
Parsing HTML with jQuery causes resources like images to be loaded because it actually uses the DOM for this action. The answer is to use another approach.