Faruz
Faruz

Reputation: 9959

ASP.Net: Get page url from inside a frame

How can I get the top page URL from inside a frame?

(in javascript it's implemented using : window.top.location)

Upvotes: 1

Views: 2324

Answers (3)

kubilay
kubilay

Reputation: 5330

In case people need it, there's a way for this certain purpose. At least I solved somehow.

First, place a button running at server, add a click event and a client-click event.

<asp:Button ID="btnFindParentUrl" runat="server" Text="Get Url!" OnClick="btnFindParentUrl_Click" OnClientClick="fillHidden();" />

Put a hidden textbox inside a div with style="display: none;" (not Visible="false" for button because if you do client can't see and fill it):

<div style="display: none;">
    <asp:TextBox ID="txtHiddenUrlField" runat="server" BorderStyle="None" Font-Size="0px" ForeColor="#F6F6F6" Height="0px" Width="0px"></asp:TextBox>
</div>

Now place javascript code of fillHidden() function:

<script type="text/javascript">
    function fillHidden() {
        document.getElementById('<%= txtHiddenUrlField.ClientID %>').value = parent.document.location.href;
    };
</script>

That's all you have to do at client side.
Let's go to the server:

protected void btnFindParentUrl_Click(object sender, EventArgs e)
{
    string parentUrl = txtHiddenUrlField.Text;
}

This way, you should be getting parent url from button in an iframe.

Anyone needs help may ask question here or to this code's post in my personal site.

Upvotes: 1

Andrew dh
Andrew dh

Reputation: 879

you can do this if the frame + parent are both on the same domain. if so, then you can obtain references to other iframes, frames or pop-ups.

from memory, try window.parent, so yours would be something like window.parent.top.location

Upvotes: 0

Guffa
Guffa

Reputation: 700910

You can't.

The fact that the page is going to be loaded into an iframe in another page is not sent in the request to the server, so it's not possible to get the ULR of the parent page, or even to determine if there is a parent page or not.

If you need that information on the server side, you have to add that information to the request, for example by including the parent page URL as a querystring parameter.

Upvotes: 3

Related Questions