Reputation:
I have an html file which intends to load XHR html files. Here is my code:
<div id='some-id'></div>
<div id='some-id-2'></div>
<script type='text/javascript'>
$('#some-id').load('some-url');
</script>
My problem is the external html file contains some javascript code which is executed after embedding it. How can I prevent this problem? (The url is cross-domain and I do not have permission to the remote domain server)
Upvotes: 1
Views: 429
Reputation: 21694
Might not be the best solution, but since you can't control the returning data -
You can load only some of the HTML, e.g. only the elements that interest you:
$('#some-id').load('http://www.some-url.com/index.html div#elementId');
Also, like apsillers mentioned, you can exclude the script:
$('#some-id').load('http://www.some-url.com/index.html :not(script)');
Or, you could remove it at return level:
$.get('http://www.some-url.com/index.html', function(data) {
$(data).find('script').remove();
$('#some-id').html(data);
});
Upvotes: 5