Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Given a string of html code, how can I go through every tag and remove ones that is not in my whitelist (in JQuery)?
- var whitelist = ['a','div','img', 'span'];
- var canvas = '<div>'+canvas_html+'</div>';
- var blacklist = ['script','object','param','embed','applet','app','iframe',
- 'form','input', 'link','meta','title','input','button','textarea'
- 'head','body','kbd'];
- blacklist.forEach(function(r){
- $(canvas).find(r).remove();
- });
- canvas_html = $(canvas).get('div').html();
- var whitelist = ['a','div','img', 'span'];
- var output = $('<div>'+canvas_html+'</div>').find('*').each(function() {
- if($.inArray(this.nodeName.toLowerCase(), whitelist)==-1) {
- $(this).remove();
- }
- }).html();
- // output contains the HTML with everything except those in the whitelist stripped off
- $(canvas).find(':not(' + whitelist.join(', ') + ')').remove().html();
Advertisement
Add Comment
Please, Sign In to add comment