4

如果当前页面在特定页面的 iFrame 中运行,有没有办法检查 PHP 或 Javascript(首选 PHP)。所以,我想要这样的东西:

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

这可能吗?

4

2 回答 2

2

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.

于 2013-05-07T21:03:56.550 回答
2

要知道页面是否在 iFrame 中,您可以使用以下命令:

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

所以这只是一个检查的问题window.top.location.hostname(只是域,而不是整个 URl。整个 URL 是 .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  */
    }
}
于 2013-05-07T21:04:47.377 回答