Javascript functions wont work. Is it my cookie? or my code? Please Help!
I cant get my cookie to work. I believe its not my javascript that is wrong. Can anyone take a look to see what i did wrong in my cookie? I have to pages dropdown1.html and dropdown2.html. Both pages have the same exact dropdown list. There is also a javascript file called script.js which makes it so you get an error if you select the same option on the second drop down list as the first. Also in the script.js i have a cookie that should run so dropdown2.html should now what was selected in dropdown1.html Can anyone see if i made a mistake in either my javascript code in my cookie? Right now I am not getting the error message if you select the same option as the first. Ill show all my code incase you need it for reference
script.js (this is the part where I cant figure out why the error message does not appear on the second dropdown menu if user selects the same option as the first)
Code:
// Get the cookie, if available.
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) { c_end=document.cookie.length; }
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}
// Set the cookie to a value, and expire in specified days.
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
// Check if prior cookie exists from prior form screen.
// if so, see if it matches the current select value.
function no_dupes(oSelect)
{
var currentValue = oSelect.value;
var isFirstRound = (oSelect.id.toUpperCase() == "THURSAM_FIRST");
var lookForCookieID = (isFirstRound) ? "ThursAM_Second" : "ThursAM_First";
var cookieID = (isFirstRound) ? "ThursAM_First" : "ThursAM_Second";
var PriorDDValue = getCookie(lookForCookieID);
if (PriorDDValue != null && PriorDDValue != "")
{
if (PriorDDValue == currentValue)
{
alert("\n Sorry, you already choose this point value from round "
+ ((isFirstRound) ? "2" : "1")
+ ". Please select another!\nThanks.");
oSelect.selectedIndex = 0;
return false;
}
}
setCookie(cookieID, currentValue, 7);
}
}
Remove the last bracket in script.js
The 2nd parameter should be enclosed in quotes (in both html documents), although this 2nd parameter never used:
Code:
no_dupes(this,'ThursAM_Second')
Thank you Fang! It worked perfectly. I have one more question for you. If I want to add a third dropdown list called dropdown3.html which give you an error if you choose the same option as the first and second dropdown. Could I do something likes this?
No, change the onchange; only the object is required:
Code:
onchange="return no_dupes(this)
and the function:
Code:
function no_dupes(oSelect) {
var currentValue = oSelect.value;
var cookieID = oSelect.id.toUpperCase();
var cookies = document.cookie.split(";");
if(cookieID == "THURSAM_FIRST") { // FirstRound
for (var i = 0; i < cookies.length; i++) { // erase all cookies
setCookie(cookies[i].split("=")[0], '', -1);
}
}
else { // another round
for (var i = 0; i < cookies.length; i++) { // check if value used
if(currentValue == cookies[i].split("=")[1]) { // duplicate value
alert("\n Sorry, you already choose this point value from " + cookies[i].split("=")[0].split("_")[1].toLowerCase() + " round. Please select another!\nThanks.");
oSelect.selectedIndex = 0;
return false;
}
}
}
setCookie(cookieID, currentValue, 7);
}
At least 98% of internet users' DNA is identical to that of chimpanzees
I was just able to try out your code and it works great! It makes a lot of sense to me now. Thank you for all the help you gave me.
I was wodering if you know much about arraylists? I have one little thing that I am not able to figure out. I have been working on it for a couple days now and i have one little issue that i cant figure out.
I created an ArrayList of multiple answers.
Code:
var ans=new Array();
ans[0]="green";
ans[1]="blue";
ans[2]="red";
Then I created a function called answers which checks to see if the answer is either correct or incorrect and a pop up message that either says correct or incorrect.
Code:
function answer(answer) {
var correct = ans[2]; // This is the correct answer
if (tooLate == 0) {
if(document.getElementById( 'answer').value == correct) {
clearInterval(i);
alert("Right Answer with " + n + " seconds remaining");
}
else{
clearInterval(i);
alert("Incorrect! The answer was " + correct);
}
}
The problem I ran into is that this answer function only checks if the answer is red. So now I want to add more questions but if I do it will go though the same answer function and will think the answer is red. So my question to you is how could I modify this so that I can add more questions that will pull them out from the arraylist depending on which question the user is on?
Incase you need it here is my html coding
Code:
<form name="myform" action="redir2.html" method="post">
<a href="javascript: submitform()"></a>
what color is the grass?
<input type = "text" name="q1" id = "answer">
</form>
<input type = "button" id = "time" onclick="answer()">
</body>
Here is my full .js file incase you need it to undersand what im doing.
Code:
var n = 16; // modify this for number of seconds to answer
document.getElementById( 'time').value = "Answer in " + n + " seconds";
var i = setInterval('count()' , 1000);
var tooLate;
function count() {
tooLate = 0;
n--;
if(n >=0) {
document.getElementById( 'time').value = "Answer in " + n + " seconds";
}
else {
clearInterval(i);
alert("Too late!");
document.getElementById('answer').value = "";
tooLate = 1;
}
}
//array of right answers
var ans=new Array(); // regular array (add an optional integer
ans[0]="green"; // argument to control array's size)
ans[1]="blue";
ans[2]="red";
function answer(answer) {
var correct = ans[2]; // This is the correct answer
if (tooLate == 0) {
if(document.getElementById( 'answer').value == correct) {
clearInterval(i);
alert("Right Answer with " + n + " seconds remaining");
}
else{
clearInterval(i);
alert("Incorrect! The answer was " + correct);
}
}
submitform();
function submitform()
{
document.forms["myform"].submit();
}
}
If your not sure its ok, I thought I would ask you because you were the only one that was able to help me with the other issue.
Bookmarks