Spenser has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use the redirect command to switch the web browser to a new frame. I have a left margin frame and a main frame. I want to redirect or change the contents of both frames. Here's my relevent code section:

use CGI qw/:standard/; my $co = new CGI; if ($x eq "9000") {     print $co->redirect(-target=>'_top', -location +=>'/intranet/subdir/index.html'); }

This works, sort of, except that I end of with two left margins--the first margin along with both the new margin and the new main frame inside of the old main frame. This is not the desired effect I was hoping for. No, instead I was hoping to replace the entire page with a new main frame and new margin.

I've searched through the Perl Monks site pretty thoroughly, but can't figure out where I'm going wrong. Incidentally, when I add -nph=>1, I get a malformed header error. I've also tried replacing "redirect" with "header"; no difference.

Any thoughts?

Edited: 24 July 2001 - 20:00 (PDT), footpad

Replies are listed 'Best First'.
Re: Redirect to New Frames
by thpfft (Chaplain) on Jul 24, 2001 at 13:45 UTC

    You can't do this with CGI, or any other server-side mechanism. HTTP works by call and response: the browser requests content from the server to fill a particular slot, and the server provides. The solution, therefore, is to make the browser ask for the other frame as well.

    Which is a (boo) javascript thing. When you return the main frame, put something like this in the header:

    <script language='javascript'> top.lefthandframe.location.href = '[url]'; </script>

    This will of course only work for people whose browsers can do (simple) javascript, and who haven't switched it off, but in most cases if the browser can do frames it can do this.

    The only alternative is to make your script write a new frameset first. That's more reliable, but if you end up with three cgi invocations to make one page, then it's time to reconsider the page design.

    and anyway, if you're changing both frames at the same time, why not just put them on one page?

Re: Redirect to New Frames
by bbfu (Curate) on Jul 24, 2001 at 13:00 UTC

    The target="_top" attribute has to be given on the original anchor or form tag that initiated the request to your CGI. You cannot, unfortunately, specify a target in a redirect header.

    It might be possible to create a hack where, instead of returning an actual redirect header, you return a page that has a zero second meta-refresh tag with a target="_top" attribute but that relies on their browser supporting meta-refresh tags, which not all do. And it's still a hack. *shrug*

    bbfu
    Seasons don't fear The Reaper.
    Nor do the wind, the sun, and the rain.
    We can be like they are.

Re: Redirect to New Frames
by Spenser (Friar) on Jul 24, 2001 at 18:40 UTC

    Marvellous! These were both helpful comments. They both were somewhat philosophical, too--very fitting for the Perl Wisdom section.

    I took the easy way out, as suggested by "thpfft" above and used the Java script you provided me. I try not to use Java Script if I can avoid it, but I'm worn out and out of time. It will have to do for now since it solved my problem.

    Thanks.