How to trap back button events with javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PiedmontBiz

    #1

    How to trap back button events with javascript

    Greetings

    I have written an online survey with 110 questions. I use javascript to cycle
    thru all the questions which are displayed in an iframe.

    I keep track of the question number using a "global" variable.
    When testing it, everything works fine until I hit the back button. The iframe
    goes to the previous question, but of course, the counter is not decremented.

    Example if I am at question # 5 and I hit the back button, I go to #4, but If I
    proceed to the next question, the count jumps to #6 rather than 4.

    I did this using cookies once- each question represented a new page generated
    by Python. This handled the back button problem just fine,

    I like the fact that Iframes greatly reduce my bandwidth usage.

    Any Ideas or do you need more details.

    Below is the Javascript I used. Simple I know. I am learning.

    //
    <script language="JavaS cript">

    var response=new Array(111);
    var scores =new Array(23);
    var rank =new Array(23);
    var toprank=new Array(23);
    var num=0;
    var numtoprank=0;
    var list=new Array(23);
    var Rnum=new Array(23);

    function zeronum()
    {
    num=0;
    }


    function rankresponse()
    {
    var temp=0;

    for (n=0;n<22;n++)
    {
    rank[n]=0;
    list[n]=n;
    }

    for (n=0; n<22;n++)
    rank[n] = response[n] + response[n+22] + response[n+44] + response[n+66]
    + response[n+88];


    for (n=0; n<22;n++)
    {
    Rnum[n] = rank[n];
    for (j=n+1; j<22;j++)
    {
    if(rank[j]>Rnum[n])
    {
    Rnum[n] = rank[j];
    rank[j]=rank[n]
    rank[n]=temp;
    temp=list[j];
    list[j]=list[n];
    list[n]=temp;
    }//end if
    } //end for j
    }//end for n




    }

    function aa(start)
    {
    var s="http://mywebsiteactual urlhere.com/testresults.php 4?";

    if (num<=108){
    num=num+1;
    response[num-1]=start;
    window.frames.a nIframe.locatio n.href
    ='statements/statement'+num. toString()+'.ht m';


    }
    else {
    rankresponse();
    //window.frames.a nIframe.locatio n.href
    ='giftresults/results'+topran k.toString()+'. htm';
    // document.write( "<hr><h1>TH e program chose, "
    +list[1]+"@"+list[2]+list[3]+"</h1><hr>");

    s=s+'g1='+list[0].toString()+'&g 2='+list[1].toString()+'&g 3='+list[2].toSt
    ring();
    document.locati on=s;


    }

    }
    </script>


    Allen


  • Fred Oz

    #2
    Re: How to trap back button events with javascript

    PiedmontBiz wrote:[color=blue]
    > Greetings
    >
    > I have written an online survey with 110 questions. I use javascript to cycle
    > thru all the questions which are displayed in an iframe.
    >
    > I keep track of the question number using a "global" variable.
    > When testing it, everything works fine until I hit the back button. The iframe
    > goes to the previous question, but of course, the counter is not decremented.
    >
    > Example if I am at question # 5 and I hit the back button, I go to #4, but If I
    > proceed to the next question, the count jumps to #6 rather than 4.
    >
    > I did this using cookies once- each question represented a new page generated
    > by Python. This handled the back button problem just fine,
    >
    > I like the fact that Iframes greatly reduce my bandwidth usage.
    >
    > Any Ideas or do you need more details.[/color]

    Have you considered a <meta> tag in the iFrame HTML that holds the page
    number of the iFrame? It could look like:

    <meta name="pageCount " content="6">

    When the user clicks on "next page", you read
    the value of the meta tag and increment by one.

    You can get at the meta tags using:

    var mTags = document.getEle mentsByTagName( 'meta');

    which makes mTags an array of all the meta tags, then cycle through
    mTags till you find the one with the page number. If it's the only
    meta tag, you could use:

    var pageNum = document.getEle mentsByTagName( 'meta')[0].content;

    This solution gets rid of the global variable, means your server is in
    control of the page count and navigation and should be browser-neutral
    as well.


    --
    Fred

    Comment

    • tonylin919@gmail.com

      #3
      Re: How to trap back button events with javascript

      It is easiest to track the question number by moving the variable to
      the page itself, rather than make it global. The <meta> tag is one way
      to store the question number, although I would have probably chosen a
      JavaScript variable instead.

      Comment

      • Tony

        #4
        Re: How to trap back button events with javascript

        It is easiest to track the question number by moving the variable to
        the page itself, rather than make it global. The <meta> tag is one way
        to store the question number, although I would have probably chosen a
        JavaScript variable instead.

        Comment

        • PiedmontBiz

          #5
          Re: How to trap back button events with javascript

          >[color=blue]
          > var mTags = document.getEle mentsByTagName( 'meta');
          >
          > which makes mTags an array of all the meta tags, then cycle through
          > mTags till you find the one with the page number. If it's the only
          > meta tag, you could use:
          >
          > var pageNum = document.getEle mentsByTagName( 'meta')[0].content;
          >
          > This solution gets rid of the global variable, means your server is in
          > control of the page count and navigation and should be browser-neutral
          > as well.
          >
          >
          >--
          >Fred
          >[/color]

          Thanks for the tip. I am using an old reference to learn JavaScript (JavaScript
          the Definitive Guide 3rd edition) I got at discount. No mention of the
          getElementsByTa gName method. I am still studying it. It will be useful for many
          things.

          Thanks to the replies, I reapproached the problem as solvable. A simple
          solution came up which seems to work. I noticed that --at least with IE -- I
          could keep track of the iframe url even when I use the back and forward
          navigation buttons.

          I then rewrote my survey function to check the current iframe url then parse it
          to find the number I use as part of the file name.

          For instance, my 110 questions are named
          statement0.htm, statement1.htm. ...statement109 .htm

          here is what seems to work.......
          (Oh I know I could be more frugal with variables, but I am an old MSbasic hack
          and run-style programmer)

          // on the form, I press a button with this as the behavior

          function aa(start) //start is a weight 0-4
          {
          var frameurl="http://myfakeurltill_I _hardenmyserver .com/testresults.php 4?";
          var s_start=0;
          var s_end=0;
          var s_num="";
          var num=0;


          s_end=window.fr ames.anIframe.l ocation.href.in dexOf('.htm');
          s_start=window. frames.anIframe .location.href. indexOf('statem ents/statemen
          t')+20;
          s_num=window.fr ames.anIframe.l ocation.href.sl ice(s_start,s_e nd);
          num=parseInt(s_ num);

          //alert(' number is '+s_num);

          response[num]=start;

          if (num<109)
          {
          num=num+1;
          window.frames.a nIframe.locatio n.href
          ='statements/statement'+num. toString()+'.ht m';

          }
          else {

          rankresponse()

          //alert(' I have reached: '+num.toString( ));

          frameurl=frameu rl+'g1='+list[0].toString()+'&g 2='+list[1].toString()+'&g 3=
          '+list[2].toString();
          document.locati on=frameurl;

          }

          }


          Thanks

          Allen

          Comment

          Working...