简体   繁体   English

如何从iframe获取浏览器的当前页面网址?

[英]How to get browser's current page URL from iframe?

Ok. 好。 This might have been asked several times but my problem is slightly different. 这可能已经被问过几次了,但是我的问题略有不同。 I have following page tab in my facebook application: 我的Facebook应用程序中有以下页面选项卡:

Facebook Page Tab Facebook页面标签

This facebook page tab has my website embedded as iframe into it. 这个Facebook页面标签将我的网站作为iframe嵌入其中。 What I want is that is to get the URL of current page inside my application. 我想要的是在我的应用程序中获取当前页面的URL。

For example, if you open above link you see facebook URL in your browser(obviously) address bar. 例如,如果您打开上面的链接,您将在浏览器(显然)地址栏中看到Facebook URL。 In my iframe I just want to retrieve the URL of the parent page in which it is embedded. 在我的iframe中,我只想检索嵌入它的父页面的URL。

I know same-origin policies in Javascript don't allow playing with cross-domain parent page's markup using javascript but I just want to retrieve the parent page URL, thats it. 我知道Javascript中的同源策略不允许使用javascript处理跨域父页面的标记,但我只想检索父页面URL,就这样。

Is that possible in ANY way? 有什么可能吗? Any way to access the address bar URL in my PHP application? 有什么方法可以访问我的PHP应用程序中的地址栏URL?

Thanks. 谢谢。

You probably don't need the “actual URL”, but only the page id, I assume …? 我想您可能不需要“实际URL”,而只需要页面ID。 That you can get by decoding the signed_request parameter that gets POSTed to your app on initial load into the iframe. 您可以通过解码signed_request参数来获得该参数,该参数在初始加载到iframe时会被发布到应用程序中。

How to “decode” it is described here, https://developers.facebook.com/docs/facebook-login/using-login-with-games#parsingsr https://developers.facebook.com/docs/facebook-login/using-login-with-games#parsingsr此处介绍了如何“解码”它

If you're using the PHP SDK, that has a method already that does this for you. 如果您使用的是PHP SDK,则该方法已经可以为您完成此操作。

在此处输入图片说明

You can use this to access it in JavaScript: 您可以使用此代码在JavaScript中进行访问:

top.location.href

"top" is better than "parent" . "top"优于"parent" Because if your iframe is itself in another iframe then parent will return that iframe's location. 因为如果您的iframe本身位于另一个iframe中,则父项将返回该iframe的位置。 "top" will return the highest location. "top"将返回最高位置。

This will be a tough one, because CORS forbids to access the outside frame: 这将是一个艰难的过程,因为CORS禁止访问外部框架:

在此处输入图片说明

The referrer doesn't help very much either. 推荐人也没有太大帮助。

If you want to use the signed_request , and want to send custom data/parameters to your app, have a look at https://developers.facebook.com/docs/appsonfacebook/pagetabs#integrating 如果您想使用signed_request并想向您的应用程序发送自定义数据/参数,请查看https://developers.facebook.com/docs/appsonfacebook/pagetabs#integrating

You can then fill the app_data parameter, and decode that in your app. 然后,您可以填充app_data参数,并在您的应用程序中对其进行解码。

Try one of these: 尝试以下方法之一:

parent.document.location

parent.window.document.location

parent.window.location

parent.document.location.href

I'm not sure if this will work on facebook though 我不确定这是否可以在facebook上使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM