Catalin
Catalin

Reputation: 11721

Read query string parameters from inside iframe

I have a web application which is inside another web application's iframe (both are done in ASP.NET MVC).

Is possible to read from the application inside the iframe the query string parameters from the parent web application using C#?

Upvotes: 3

Views: 18163

Answers (3)

user3682349
user3682349

Reputation: 11

You should use %26 instead of & to send to iframe, for instance:

Link : 2.asp?ppp=/Payam.asp?sid=1%26PID=1&sa=12313 <br>

In code : when I requset.queryString("ppp") in the parent it shows: /Payam.asp?sid=1&PID=1

You can use this as source of your iframe, it has two parameters:

<IFRAME name="My"  src='<%=ppp %>'></IFRAME> 

Upvotes: 0

gideon
gideon

Reputation: 19465

window.parent should give you a reference to the Parent.There is also a window.top which gives the immediate parent so if your application is nested more than once you should use the right property.

From there you can do window.parent.location (or window.top.location) which has various properties of the URL requested.

To actually read the query string there are posts about that here. You just need to replace window.location with window.parent or window.top :

See the answer here : How to get the query string by javascript? and much more detailed answers here: How can I get query string values in JavaScript?


In the Controller : Since you said you need the query strings in the controller. You'll need to still get the query string like I've described above, then you'll pass it into the controller by putting it in the call to the src of the inner frame.

<iframe src=""></iframe>

It all depends on how and when you're setting the src attribute of your inner frame. You need to somehow change it at some point.

From your outer frame you could do:

document.getElementById('myframe').src
 = "http://innerApplication/innerController/actionmethod/?" 
   + window.location.search;

Or, from inside the frame itself, you'll get the query string first, then do a redirect/reload by doing window.[top/parent].location = the url + query string

Then you'll read the query string as if it were the query string of the inner application.

Upvotes: 1

sandip
sandip

Reputation: 70

You have to append whatever data you want to read from parent page from the page within iframe to the iframe url itself. For example:

<iframe id="yourid" src="yourpage.aspx?parentdata=whatever_data_to_be_captured">
</iframe>

Now, you have to parse the src value from your iframe page to capture data.

This is a trick, and as far as I know there seems to be no direct way to access parent page data from iframe.

Upvotes: 5

Related Questions