简体   繁体   中英

Disable Javascript from cross-domain file

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)

Might not be the best solution, but since you can't control the returning data -

You can load only some of the HTML, eg 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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM