user198989
user198989

Reputation: 4665

How to stop javascript function which is from iframed content?

I am iframing another website. But there is a javascript function on their page that I don't want to execute on my page. It's

if (top.location != location) {
top.location.href = document.location.href; }    

I tried this, but it stops whole the page.

function StopLoading() {
if (!document.all)
{
window.stop();
}
else
{
window.document.execCommand('Stop');
}
}    

<iframe id="i_frame" onload="StopLoading()" src="http://website.com/"></iframe>

Upvotes: 0

Views: 125

Answers (2)

Some Guy
Some Guy

Reputation: 16210

As I've said in an earlier answer of mine, you can do this only if you have a server that will be able to return the HTTP/1.1 204 No Content header. If you have that kind of a server, then you could use code like this:

var prevent_bust = 0;
window.onbeforeunload = function (){
    prevent_bust++;
};
setInterval(function (){
  if (prevent_bust > 0) {
    prevent_bust -= 2;
    window.top.location = 'http://server-which-responds-with-204.com';
  }
}, 1);

This method is described here.

Without the 204 header, you probably won't be able to do this. And for good reason. Frame-busting is usually used to prevent clickjacking, which can be very harmful.

Upvotes: 0

ziesemer
ziesemer

Reputation: 28687

I don't believe you can do this, due to standard browser security restrictions (assuming the 2 sites involved are on different domains). The ability to do this would open up possibilities for rather nasty things. Should you be putting this other site in an iframe in the first place?

Upvotes: 2

Related Questions