1

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)

5
  • use ajax and inject via $('#some-id')[0].innerHTML= instead of .html() (called internally by load) Commented Jul 9, 2013 at 15:29
  • @Samuel, does the content from some-url have a single top-level element? If that's the case, you can specify a selector matching that element after the URL and load() will strip the scripts. Commented Jul 9, 2013 at 15:30
  • Does loading the external file work at all (and if so, how are you working around SOP)? What's wrong with executing the scripts? Commented Jul 9, 2013 at 15:32
  • Hamidi, what do you mean by top-level element? Commented Jul 9, 2013 at 15:33
  • Bergi, all things went well. What I want to do is only to disable Javascript from remote source. Commented Jul 9, 2013 at 15:35

1 Answer 1

5

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);
});
Sign up to request clarification or add additional context in comments.

3 Comments

No use because the javascript code remote has been executed before setting this command.
If it doesn't get inserted into the DOM, it shouldn't be executed - try the $.get method
Still the same when I use get method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.