javascript - How can I detect if an iFrame gets redirected to another url? - Stack Overflow

I want to list an iframe with a classified ad in it. Sometimes the vendors redirect the page to their h

I want to list an iframe with a classified ad in it. Sometimes the vendors redirect the page to their homepage when the item is no longer available. I want to be able to identify this so I can de-index the item assuming it has sold. What is the best method to acplish this?

I want to list an iframe with a classified ad in it. Sometimes the vendors redirect the page to their homepage when the item is no longer available. I want to be able to identify this so I can de-index the item assuming it has sold. What is the best method to acplish this?

Share Improve this question edited Aug 14, 2015 at 15:53 Dave Powers 2,4032 gold badges35 silver badges37 bronze badges asked Dec 18, 2012 at 0:26 user782993user782993 851 gold badge2 silver badges4 bronze badges 1
  • From what I can tell, this isn't possible. You can only do this if the iframe refers to content hosted on your domain. – Blender Commented Dec 18, 2012 at 0:30
Add a ment  | 

3 Answers 3

Reset to default 2

You can use jQuery's load function that fires onload or reload.

http://api.jquery./load/

You will need to store the number of times the iframe has reloaded.

Something like,

var reloaded = 0;
$("#iframe_id").load(function(){
    reloaded++;
});

You can not detect the location of the iframe when it is in a different domain unless they have the proper headers set for CORS.

The only way to do it without worrying about the other domain is to have your serverside code run a check on the page and see if it redirects.

On the frame load, detect what the URL is:

var frameLoadCount = 0;
var firstURL;

$('iframe').load(function() {
    if (frameLoadCount === 0) {
        firstURL = $(this).attr('src');
    } else {
        if (firstURL !== $(this).attr('src')) {
            // The iframe has been redirected.
        }
    } 
});

Basically the above code runs everytime the iframe has loaded, checks if it's the first time, if it is, assign the URL to a variable. If it has loaded more than once, check if the original url matches the current url.

This has benefits from just checking if it's loaded more than once, because you can target specific sources etc.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745471404a4629151.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信