views:

1976

answers:

1

This is kind of a weird problem, but I have to create a search box for our site that will be iframed on another site. When the user clicks the search button, it needs to redirect the parent frame to our search results page.

At the moment what I've done is to make the search button a postback trigger then registering a client script block to run this:

if (window.parent) window.parent.location.href='<url>';

This is ok, but it seems like a hack & it means if the user clicks back on the browser after searching, it redirects them back to the search results page.

Is there a better way of doing this?

+1  A: 

OK, since noone else has answered, I'll give this a go.

First off, are you testing the code you're using in a real-world way? I am concerned that in a real-world application that your iframed page and the parent page will not reside on the same domain. If so, there is not likely to be any way for your code to direct the parent window to any URL.

If you are allowed to control the parent frame's location, your code looks fine to me. If you don't want the new URL in the history, I believe that you can use:

if (window.parent) { window.parent.location.replace("<url>"); }

See for example: "The Difference Between Javascript Redirect Methods"

J5
It's ok, in the end they've gone for having the page open in a completely new window anyway, but thanks anyway!
Glenn Slaven