Link to home
Create AccountLog in
Avatar of bt707
bt707Flag for United States of America

asked on

Perl sub in html code

I have some web cgi's, mostly perl with some html and javascript part in them.

I have a section that runs that has html in it, which it calls a javascript session timer by using $QandASessionTimer in the html code it will then call the timer code which is shown below in the attached code.

Not sure how to ask this since I cannot post the whole thing.

What I'm trying to do is to put the $QandASessionTimer into a subroutine and call it with &QandASessionTimer rather than $QandASessionTimer

Can anyone give me a clue why in the html part I can use $QandASessionTimer but not &QandASessionTimer to call a sub?

Thanks,
$QandASessionTimer.=<<"QATIMERSCRIPT";
<script type="text/javaScript">
var starttime = 1200;
var endtime = 0;

function timechecker() {

    if (starttime >= endtime) {

      setTimeout('countdowner()', 1000)
    }
    else {
      alert("Your session has timed out you will now be returned to the login page.");
      window.location = "$RedirectLocation";
      }
  }

  function countdowner(){
      var mins = Math.floor (starttime /60);
      var secs = Math.floor (starttime - (mins*60));
      if (secs < 10) {
        secs = '0' + secs;
      }
      var display = '<center><font size=1>Session timeout in:<\\/font><br \\/>' + mins + ":" + secs + '<center>';

      document.getElementById('clock').innerHTML = display;
      starttime = starttime - 1;

      timechecker();

    }
timechecker();
</script>
<span id='clock'></span>
QATIMERSCRIPT

Open in new window

Avatar of jeromee
jeromee
Flag of United States of America image

bt707,
what is that  &QandASessionTimer sub that you are talking about?
Avatar of bt707

ASKER

the code above which is:

$QandASessionTimer.= ......

I want to put all of that in a sub of

sub QandASessionTimer {

}

the problem I have is the $QandASessionTimer is in a bit of HTML code, so the $QandASessionTimer is really a parameter that is passed to the HTML code and executes a timer that way, but the timer is used in multiple places and needs different settings, so I want to put it in a sub that can be called instead of making a several different $QandASessionTimer.=xxxx's

So the HTML code will execute it when put in the HTML code as a variable but cannot see how to put it in a sub and call and run the sub from the HTML, hope that makes more sense.

Thanks,
You could put the javascript into a separate file, say timer.js with this code inside:
<script type="text/javaScript">
var starttime = 1200;
var endtime = 0;

function timechecker(goToUrl) {

    if (starttime >= endtime) {

      setTimeout('countdowner()', 1000)
    }
    else {
      alert("Your session has timed out you will now be returned to the login page.");
      window.location = goToUrl;
      }
  }

  function countdowner(){
      var mins = Math.floor (starttime /60);
      var secs = Math.floor (starttime - (mins*60));
      if (secs < 10) {
        secs = '0' + secs;
      }
      var display = '<center><font size=1>Session timeout in:<\\/font><br \\/>' + mins + ":" + secs + '<center>';

      document.getElementById('clock').innerHTML = display;
      starttime = starttime - 1;

      timechecker();

    }
</script>
<span id='clock'></span>

Open in new window


You would then include timer.js in all your HTML files that need it.
Then, where ever you need to call the timer, you could call it with:
    timechecker("http://some/url");
Avatar of bt707

ASKER

I guess I'm not explaining it very good, I need to put the code that is in $QandASessionTimer.=.....
in a sub then call the sub instead of using the variable of $QandASessionTimer

I haven't found a way to do that from the way the code is but have been told to do it and it can be done.

The CGI's we use is very long and cannot post the whole thing but I've attached a text file that has these parts in it.

Please see the file attached, it has the timer variable at the top and below that is a sub that is in the CGI that calls the $QandASessionTimer and uses it, this way you can see how it currently works and have been using for years, but need to change it to a sub and call it from same place somehow.

In the attached file the timer variable is at the top, then there is one sub under that, if you do a search for $QandASessionTimer it will show how it is called and used right now.

Thanks for any help or ideas on what can be done.
timer.txt
Sorry, but I still understand why you would want to use a sub instead of what you have and therefore I probably don't understand what you are trying to achieve.
Sorry.
Avatar of bt707

ASKER

Was trying to put the timer code into a sub instead of a variable because the timer is used for several different pages in which the timer setting is different, so the sub can be called with a timer argument for each one, that way the sub can be managed from one sub instead of making a duplicate of the code for each one.

I can put it in a sub but cannot find a way to call it the same way the variable is called from the html code.
ASKER CERTIFIED SOLUTION
Avatar of FishMonger
FishMonger
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
If this data needs to be appended to the existing var, then change the beginning of the sub to this:
sub QandASessionTimer {
    my $RedirectLocation = shift;
    my $prior_content = shift;
    
    return <<"QATIMERSCRIPT";
$prior_content

Open in new window


And call it like this:
QandASessionTimer( $redirect_to_location,  $QandASessionTimer );
Avatar of bt707

ASKER

Thanks all for the help, I think this will help and may be able to get it working now.