I have:
$('body').data('data1', '<script type="text/javascript">console.debug('execute');</script><div>example</div>');
and:
<div id="content"></div>
when I do:
$(document).ready(function () {
$('#content').html($('body').data('data1'));
});
then JavaScript is executed. How to prevent executing? Is it possible?
You have to strip the
scripttags from your HTML string.This works with Firefox/Chrome, although I don’t know about other browsers. Note this will only work with well-formed (x)html.
EDIT: If you also want the JS, you can amend the previous code thus:
Mind you, you don’t even have to remove the
scripttags. Now that the response is in its separate DOM document, it will not mess up your own scripts, and you can access whatever content you need from it using easy$('selector', doc)jQuery.