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

Hello Monks,

I'm trying to incorporate a simple "open window" Javascript into my Perl script. I'm having a little trouble.

The first problem involves the Close Window routine. When I click the button to close the window; every window closes not just that one. This is what happens when I test outside the Perl/CGI script.

Here's the Javascript code for Close Window.
<INPUT type="button" value="Close Window" onCLick="window.close();">


When I incorporate all of the Javascript into my Perl/CGI script, the phrase "Shipping and Tax" does not appear as a hyperlink. The way this open window should behave is when a person clicks the "Shipping and Tax" link, the new window should open, the person then can close that window, and be returned to the previous window. Instead, I'm totally getting kicked out and I can't create the "Shipping and Tax" link.

Here's the first bit of Javascript code that defines the function for open window.
# Javascript to create popup window my $JSCRIPT; $JSCRIPT=<<EOF; function launchwin() { window.open("../shiptax.htm", "dialog", "width=300, height=250, scroll +bars=yes, resizable=yes"); } EOF ;

This piece of code calls the Javascript.
print header (-cookie => $cookie), start_html (-title => "Test Site", -script=>$JSCRIPT, -backgro +und=>"../images/sb0068.jpg", -leftmargin=>"0", -topmargin=>"0"), add_boilerplate ($page), end_html ();

Finally, this piece of code should have the phrase "Shipping and Tax" as a hyperlink, when clicked, should open the window.
$page .= p({ -style => "font-family: verdana; font-size: 10pt;" }, "Sh +ipping and tax may be added to your order.\n" . "To estimate shipping and tax, please see the " . a({-onClick => "launchwin"}, "Shipping and Tax") . " page.\n");

I hope someone can give me some help.
Thanks.

Replies are listed 'Best First'.
Re: Perl/CGI/Javascript Problem
by pfaut (Priest) on Mar 04, 2003 at 16:42 UTC

    An anchor (A) tag needs a HREF attribute to be considered a link. If you want to execute javascript code on a click, don't use an anchor tag. Use a SPAN tag with style settings to make it stand out as a link. The anchor tag can take a TARGET attribute that will open a new window if the named target window doesn't already exist.

    Remember that anchor tags can also create named anchors within pages with the NAME attribute. These anchors don't get any special visual effects and don't react to mouse clicks.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
      Hi pfaut,

      I tried to incorporate your suggestion by using the SPAN tag. I had no luck. I pasted the modified code with the new tag. Can you take a look and see where I went wrong?

      Thanks.

      $page .= p({ -style => "font-family: verdana; font-size: 10pt;" }, "Sh +ipping and tax may be added to your order.\n" . "To estimate shipping and tax, please see the " . span({-style => "background-color:gray;" -onClick + => "launchwin"}, "Shipping and Tax") . " page.\n");
        try this
        span({-style => "background-color:gray;", # added comma -onClick => "launchwin()"}, # added ()
        poj
      You can use an A tag, as:
      <a href='javascript:void(0);' onClick='do_something();'>Click here</a>