【问题标题】:Detect denied access to iframe url检测拒绝访问 iframe url
【发布时间】:2016-06-30 10:27:45
【问题描述】:

我正在尝试检查 iframe 的 url,但我刚刚意识到我不需要实际的 url,只需要有关 url 是否在我自己的域内的信息。

我想我可以使用

document.getElementById("frameid").documentWindow.location.href

如果 url 不在我的域中,它将返回错误。但是如何拦截这个错误呢?

【问题讨论】:

标签: javascript url iframe


【解决方案1】:

我收到的评论中的帖子很有帮助,但我自己没有找到该帖子,所以我想我会在这里发布我的最终解决方案,以防其他人来到这里有类似的问题。

$('#iFrame').on('load', function () {
    try {
        // checks if iframe content is internal
        test = this.contentWindow.document.location.href;
        showIframe = false;
    }
    catch (err) {
        // if an error is thrown, iframe content is external
        showIframe = true;
    }

    if (showIframe) {
        // do some stuff
    }
}

【讨论】:

    猜你喜欢
    • 2012-05-15
    • 2015-07-01
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多