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

I need to use a javascript function with this open the output in a new window of a certain size. I thought I could do it with a simple target statement on the html/javascript side. Of course it puts the data output into a new window but will not size the window, even with correct height and width attributes. Any ideas, how I can do that in this simple cgi?
#!/usr/bin/perl require "cgi-lib.pl"; $match="no"; &ReadParse; print &PrintHeader; open (FILE,"fake.txt") || die "Can't find database\n"; @indata = <FILE>; close (FILE); print <<"PrintTag"; <html> <head> <title>Search Results</title> </head> <body BGCOLOR=#FFFFFF> <table border=1 align="center"> <tr> <th>Date</th> <th>School 1</th> <th>Score 1</th> <th>School 2</th> <th>Score 2</th> <th>Where</th> </tr> PrintTag foreach $i (@indata) { chop($i); ($ID,$Date,$Where,$School1,$Score1,$School2,$Score2) = split(/\|/,$i); if ($School1=~/$in{'School1'}/i) { $match="yes"; print "<tr>"; print "<td>$Date</td>"; print "<td>$School1</td>"; print "<td>$Score1</td>"; print "<td>$School2</td>"; print "<td>$Score2</td>"; print "<td>$Where</td>"; print "</tr>\n"; } } print "</table>"; if ($match eq "no") { print "<p>Sorry. No matching records were found.</p>"; } print "</body></html>"; #end

Replies are listed 'Best First'.
Re: Need to use Javascript function in Code
by bart (Canon) on Aug 29, 2002 at 07:16 UTC
    Of course it puts the data output into a new window but will not size the window, even with correct height and width attributes. Any ideas, how I can do that in this simple cgi?
    It's too late. You have to open a window for that target name on the client side, with Javascript, before opening the URL of the script in it. This means you have to do it in an "onclick" event. There are two ways: open a new window with this URL, or just open a window (with any URL) and use the "href" attribute for the URL.

    I tend to use both at the same time, so that it still sort of works, if Javascript is disabled: it'll do the same thing you see now, then. By making the "onclick" event return false one can prevent the CGI script from being called twice.

    We've navigated completely out of the realm of Perl now, and what remains is a pure HTML/Javascript problem. However, I would feel bad for stopping here, so I'll stick out my neck and give you some HTML/Javascript to play with:

    <script language="JavaScript"><!-- function openBrowserWindow(theURL,winName,features) { var w = window.open(theURL,winName,features); w.focus(); } //--></script> <a href="scripturl.cgi" onClick="openBrowserWindow('scripturl.cgi','sc +ripttarget','width=400,height=300'); return false;" target="scripttar +get">results</a>
    Note the redundancy in URL and window target name. One is for Javascript enabled, one for Javascript disabled browsers.
Re: Need to use Javascript function in Code
by davis (Vicar) on Aug 29, 2002 at 09:23 UTC

    Preface: This isn't meant to be a personal attack, just some advice which will almost certainly help you in the future

    Please, Please don't use this code. cgi-lib.pl is considered well and truly buried, and with good reason. Check out the following for (some) reasons, and the alternatives: use CGI or die;, No excuses about not using CGI.pm, and you would benefit from reading Ovid's Web Programming with CGI.
    The above is a truncated list, and there's plenty of resources around here to help you.

    On the more positive side, here's some code which (AFAICT) does what your code does, but will hopefully be more easy to debug, extend, etc.
    #!/usr/bin/perl use warnings; use strict; use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser); my $database_file_name = "fake.txt"; print header(); print start_html(-title=>"Search Results"); #Open and read in the file open(DATABASE, "<", $database_file_name) or die "Unable to open $database_file_name: $!\n"; my @input_data = <DATABASE>; close(DATABASE); #Get the supplied search term. my $school_search_term = param("School1"); #The HTML table header my @table_rows = th(["Date", "School 1", "Score 1", "School 2", "Score + 2", "Where"]); #Loop through each record, looking for a match. my $num_matches = 0; foreach my $record (@input_data) { my ($id, $date, $where, $school_1, $score_1, $school_2, $score +_2) = split(/\|/, $record); if(lc($school_search_term) eq lc($school_1)) { #We've found a match $num_matches++; push @table_rows, td([$date, $school_1, $score_1, $sch +ool_2, $score_2, $where]); } } #Print the results table, if necessary if($num_matches) { print table(-caption=>"Search Results", Tr(\@table_rows)); print "Found $num_matches matches", br() } else { print b("I'm sorry, no matches were found"), br(); } #Close the html page print end_html;

    If you must use JavaScript, then you can use the onLoad, onFocus, etc., attributes to the start_html function. I'm not totally certain about that, because I gave up using JavaScript a long time ago. Check out perldoc CGI for the real answer.

    Cheers
    davis
    Is this going out live?
    No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist
Re: Need to use Javascript function in Code
by screamingeagle (Curate) on Aug 29, 2002 at 07:18 UTC
    try the window.open function (e.g. add this between the <HEAD> and the </HEAD> tags):
    my $code = <<EOF; <script language=javascript> var newwin = window.open('output.html' , 'Windowname', 'location=0,me +nubar=0,resizable=1,height=350,width=500,innerHeight=350,innerWidth=5 +00,scrollbars=1,toolbar=0,titlebar=0'); newwin.focus(); </script> EOF ; print $code;
Re: Need to use Javascript function in Code
by dwatson06 (Friar) on Aug 29, 2002 at 14:23 UTC
    I have several web applications that I resize the window on also. I use an onClick resize when the window opens but I also use an onLoad to resize the loading page. That way if another developer wants to create a link to the script and they forget to resize with the OnClick, you know that the page will resize itself. Resizing when the page loads initially opens a normal size window but then resizes when the javascript executes.
    Daniel