Hammad
Hammad

Reputation: 2137

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 Page Tab

This facebook page tab has my website embedded as iframe into it. What I want is that is to get the URL of current page inside my application.

For example, if you open above link you see facebook URL in your browser(obviously) address bar. In my iframe I just want to retrieve the URL of the parent page in which it is embedded.

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.

Is that possible in ANY way? Any way to access the address bar URL in my PHP application?

Thanks.

Upvotes: 0

Views: 2501

Answers (4)

Tobi
Tobi

Reputation: 31489

This will be a tough one, because CORS forbids to access the outside frame:

enter image description here

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

You can then fill the app_data parameter, and decode that in your app.

Upvotes: 0

C3roe
C3roe

Reputation: 96455

You probably don’t need the “actual URL”, but only the page id, I assume …? That you can get by decoding the signed_request parameter that gets POSTed to your app on initial load into the iframe.

How to “decode” it is described here, 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.

Upvotes: 2

user3811473
user3811473

Reputation: 92

enter image description here

You can use this to access it in JavaScript:

top.location.href

"top" is better than "parent". Because if your iframe is itself in another iframe then parent will return that iframe's location. "top" will return the highest location.

Upvotes: 1

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

Upvotes: -1

Related Questions