duanla8800
2013-05-07 21:00 阅读 36
已采纳

PHP / Javascript检查是否在特定的iframe中运行[重复]

This question already has an answer here:

Is there a way to check in either PHP or Javascript (preferrable PHP), if the current page is running in an iFrame on a specific page. So, I'd like something like this:

if(runningInIframeFromPage("http://www.myAwesomeWebsite.com")){
    //go on with script
}

Is this possible?

</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    doukuanghuan7582 doukuanghuan7582 2013-05-07 21:04

    To know if the page is in an iFrame, you can use this:

    if (window!=window.top) { /* iframe */ }
    

    So it's just a matter of checking with window.top.location.hostname (just the domain, not entire URl. For entire URL is .href)

    if (window!=window.top) {
        if(window.top.location.hostname = "www.myAwesomeWebsite.com" || window.top.location.hostname = "myAwesomeWebsite.com" ){
            /* code if domain is myAwesomeWebsite.com or www.myAwesomeWebsite.com  */
        }
    }
    
    点赞 评论 复制链接分享
  • duanmeng9336 duanmeng9336 2013-05-07 21:03

    Part of this question has been answered before:

    if (parent==top) { // ...
    

    With regard to the PHP part - no, this is not possible, however you could always use the JavaScript check to fire off an Ajax request to dynamically load in the content you want.

    点赞 评论 复制链接分享

相关推荐