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

hi brothers, this seems to be one interesting situation but i can't figure out what's going on. IS there any way to solve this?

problem :
if a window (popup) already exist and if window.open is called again, it will not load the contents of the perl at all! I need the popup window to always have the latest data whenever decides to open the window.

to test :
1.) execute testopener.pl
2.) click on the open window button, a popup appears with a randomly generated number. Remember the number.
3.) go back to testopener.pl page, hit refresh (so that it'll clean up all the window variable).
4.) now click on open window button again, you'll notice that the number did not change! (but it's actually calling window.open)

firstly, have a file called :
testperl.pl :
#!/usr/bin/perl print "content-type: text/html \n\n"; print int(rand(1000));

then..
testopener.pl :
#!/usr/bin/perl print "content-type: text/html \n\n"; print<<OUTPUT <HTML> <SCRIPT> var winobj; //is there no way to do this beside declaring a global? function popWindow(url){ var width = 700; var height = 500; var left = (screen.width - width)/2; var top = (screen.height - height)/2; var params = 'width='+width+', height='+height; params += ', top='+top+', left='+left; params += ', directories=no'; params += ', location=no'; params += ', menubar=no'; params += ', resizable=no'; params += ', scrollbars=yes'; params += ', status=no'; params += ', toolbar=no'; //refresh if window is still open if(winobj && !winobj.closed){ alert('reloading..'); winobj.location.href = url; }else{ alert('opening as new..'); winobj=window.open(url, 'x', params); } if (window.focus) {winobj.focus()} return false; } </SCRIPT> <BODY> <FORM ACTION="#"> <INPUT TYPE="BUTTON" VALUE="OPEN WINDOW" ONCLICK="popWindow('t +estperl.pl');"/> </FORM> </BODY> </HTML> OUTPUT

Replies are listed 'Best First'.
Re: PERL + javascript popup
by dhosek (Beadle) on Mar 05, 2008 at 05:52 UTC
    If you haven't already, I would suggest installing firefox and the firebug extension which will allow you to run the code in a debug window. But the most likely scenario that's going on here, is that the page is being cached in the browser. Try adding a random number as a parameter to the url (i.e., use url + '?X=' + Math.random() in place of url) and see if that makes the problem go away.
    Donald Hosek, Tech Lead at oversee.net
    L.A. perl people, we're hiring.
      dhosek, you might be right, but i restarted my pc, cleaned my cache and it didn't work. then i went ahead to try appending a random number besides its url and it worked just fine! peculiarly though, im actually using IE 7.0.5730.11, while my friend is using 7.0.5730.11RC and he didn't had any prob without the random numbers. Firefox also worked fine.
        Yeah, it's a browser-dependent thing. IE tends to be the most egregious offender in the unintended caching thing, as it tends to ignore nocache directives (but not, as you noticed, consistently).

        The other option to consider would be to do this through an Ajax call: Set up a div to hold the updated info, and have the button action do an XHR to update the div. I would suggest not writing the code directly, but using a pre-existing library (jquery and yui are relatively lightweight, I'm partial to prototype, my JS guy from a previous job swore by mootools, it's all religion ultimately). Once you start doing the Ajax-y stuff, you discover that the old way of doing things is just painful.

        Donald Hosek, Tech Lead at oversee.net
        L.A. perl people, we're hiring.
Re: PERL + javascript popup
by ikegami (Patriarch) on Mar 05, 2008 at 07:58 UTC
    If caching is the issue, then using the following should fix it:
    #!/usr/bin/perl use CGI qw( ); my $cgi = CGI->new(); print $cgi->header( -type => 'text/html', -expires => 'now', # Instruct client not to cache this document. ); print int(rand(1000));