Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Quiz and Page Redirect w/ setTimeOut Problems
Old 09-06-2008, 07:34 PM Quiz and Page Redirect w/ setTimeOut Problems
Skilled Talker

Posts: 70
Location: Atlanta, GA
Trades: 0
I figure that I can't be that far off. This one should be solved quickly
pending the advice. There's something that I can't see or I'm overlooking.

This is a quiz that checks you answer, and if correct redirects you to a page
- if wrong redirects you to the wrong page. It has a time limit of 5 secs
that redirects you to a 'timeup' page once 5 secs passes.

Here's the code:

Code:
<html><head><title>Problem9</title>
<script type="text/javascript">

function attachHandlers()
{
  var the_button = document.getElementById("chk_ans");

  the_button.onclick=checkAnswer;
  
}

function attachTimeOut()
{
  var the_tag = document.getElementsByTagName("a")[0];
  
  the_tag.onclick=userTimeout
}

function userTimeout(){
  
var the_timeout = setTimeout("redirect();", 5000");

}

function checkAnswer()
{
  
  var the_answer = document.getElementById("right_ans");
  var the_opts = document.getElementsByTagName("input");

  for (var i=0; i<the_opts.length; i++)
  {

    if (the_opts[i].nodeName.toLowerCase()=="input")
    {
      var the_sel = the_opts[i].checked;
    }
  }

  if (the_sel==the_answer)
  {
    window.location.href="correct_page.html";
  }
  else
  {
    window.location.href="wrong_page.html";
  }

  setCookie(the_sel);
}

function setCookie(user_sel){
  
  var user_answer = user_sel.value;
  var the_cookie = user_answer;
  document.cookie = "my_answer=" + escape(the_cookie);
}

function redirect()
{
window.location.href="time_up.html";
}

</script></head>
<body onLoad="attachHandlers();">

<p><a href="#">Click here once ready</a></p>

<div id="the_quiz">
    <p>What's the capital of Illinois?</p>
    <input type="radio" name="choice" value="Spring"
id="right_ans">Springfield<br>
    <input type="radio" name="choice" value="Chicago">Chicago<br>
    <input type="radio" name="choice" value="Lans">Lansing<br>
    <input type="button" id="chk_ans" value="Check">

</div>

</body>
</html>
I'm hitting the 'check' button and nothing's happening. Any suggestions would be appreciated. Thanks.
LayneMitch is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-07-2008, 03:26 PM Re: Quiz and Page Redirect w/ setTimeOut Problems
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Extra quote mark after 5000 in the userTimeout function
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-07-2008, 08:01 PM Re: Quiz and Page Redirect w/ setTimeOut Problems
Skilled Talker

Posts: 70
Location: Atlanta, GA
Trades: 0
Quote:
Originally Posted by chrishirst View Post
Extra quote mark after 5000 in the userTimeout function
Got rid of that and the checkAnswer() worked, but when I send to another page depending on the answer, the other page doesn't read the answer that was selected. The difficult time I'm having is discovering whether the answer that is selected isn't being read on the quiz page, or the correct answer/wrong answer pages.

Here's the code for the correct answer page. The wrong answer has the same code.

Code:
<html><head><title>Correct Page</title>
<script type="text/javascript">

function readCookie(the_info){

  if(document.cookie)
  {
   var the_cookie = unescape(document.cookie);
   var broken_cookie = the_cookie.split("=");
   var the_prop = broken_cookie[0];
   var the_value = broken_cookie[1];
   the_info[the_prop] = the_value;
  }
  
  return the_info;
}

var cookie_info={};
cookie_info=readCookie(cookie_info);

</script></head>
<body>

<script type="text/javascript">

document.write("You selected "+cookie_info['my_answer']+" and that was correct!!");

</script>
</body>
</html>
Please let me know what you think. Thanks Chris.
LayneMitch is offline
Reply With Quote
View Public Profile
 
Old 09-07-2008, 08:47 PM Re: Quiz and Page Redirect w/ setTimeOut Problems
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
this code
Code:
  for (var i=0; i<the_opts.length; i++)
  {

    if (the_opts[i].nodeName.toLowerCase()=="input")
    {
      var the_sel = the_opts[i].checked;
    }
  }

  if (the_sel==the_answer)
  {
    window.location.href="correct_page.html";
  }
  else
  {
    window.location.href="wrong_page.html";
  }

  setCookie(the_sel);
}
sets the cookie AFTER the redirect will have happened (not usually very effective
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-10-2008, 01:00 AM Re: Quiz and Page Redirect w/ setTimeOut Problems
Skilled Talker

Posts: 70
Location: Atlanta, GA
Trades: 0
Thanks Chris,

Sorry for the late response. The problem has been solved and your advice helped. Thanks.
LayneMitch is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Quiz and Page Redirect w/ setTimeOut Problems
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.21088 seconds with 12 queries