Output the following to a new window instead of an alert box

Discussion in 'JavaScript' started by KC TAN, Jan 18, 2006.

  1. #1
    Hi all,

    I have the following codes currently to show the user's results once they answer the quiz:
    
    <SCRIPT LANGUAGE="JavaScript">
    var ans = new Array;
    var done = new Array;
    var yourAns = new Array;
    var score = 0;
    
    ans[1] = "a";
    ans[2] = "d";
    ans[3] = "c";
    ans[4] = "b";
    ans[5] = "c";
    ans[6] = "a";
    ans[7] = "d";
    
    function Engine(question, answer) {
    yourAns[question]=answer;
    }
    
    function Score(){
    var answerText = "Quiz Results\n\n";
    for(i=1;i<=7;i++){
       answerText=answerText+"Q"+i+": ";
      if(ans[i]!=yourAns[i]){
        answerText=answerText+"Wrong! The correct answer is "+ans[i]+".        \n";
    	
      }
      else{
        answerText=answerText+"Correct!\n";
        score++;
      }
    }
    
    answerText=answerText+"\nYour score is "+score+" out of "+(i-1)+"!        \n";
    
    alert(answerText);
    }
    </script>
    
    Code (markup):
    My quiz questions are like:
    
     Q1) Question here<p>
    	<input type=radio name="q1" value="a" onClick="Engine(1, this.value)"> a) answer a<br>
    	<input type=radio name="q1" value="b" onClick="Engine(1, this.value)"> b) answer b<br>
    	<input type=radio name="q1" value="c" onClick="Engine(1, this.value)"> c) answer c<br>
    	<input type=radio name="q1" value="d" onClick="Engine(1, this.value)"> d) answer d<br><p>
    
    Code (markup):
    Currently, when I press the submit button, it prompts an alert box with the results..but as highlighted in another thread, I need to open a new window and display the results there :rolleyes:

    Is there anyway that I can do so by passing the variables after the user clicks the submit button (at the same time when the Score() is executed)?

    Any help / advise is greatly appreciated.
    Thank you.
     
    KC TAN, Jan 18, 2006 IP
  2. dave487

    dave487 Peon

    Messages:
    701
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This probably can be done but I can see it causing problems with pop-up blockers.

    Why not use document write to print the result dynamically to the page at the end of the quiz?
     
    dave487, Jan 18, 2006 IP
  3. KC TAN

    KC TAN Well-Known Member

    Messages:
    4,792
    Likes Received:
    353
    Best Answers:
    0
    Trophy Points:
    155
    #3
    Hi Dave487,

    In addition to the results, I also need to include a link so that user can link to a relevant section on that particular question (if he / she got it wrong). Printing all these at the end of the page is not as user friendly as opening another small window in my opinon.

    I have looked around for some alternatives, one is to do a window.open. I will try out and see what i can get..

    By the way, I believe that the pop-up blocker won't block window.open, right?
     
    KC TAN, Jan 18, 2006 IP
  4. torunforever

    torunforever Peon

    Messages:
    414
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #4
    By using window.open, you'll be able to modify the contents of the new window, or if the window.open is calling an existing page, then you'll be able to call functions in that page, so I agree that you should try it.

    Most pop-ups are as a result of window.open being called, so of course pop-up blockers will block them. The only thing you have to your advantage is that the user is initiating the window.open call (as opposed to the popups being called onload). Some pop-up blockers allow this, some don't, and other are configurable to block this.
     
    torunforever, Jan 19, 2006 IP