Click to See Complete Forum and Search --> : an error i can't seem to find on line 1


akninshar
08-20-2006, 04:18 PM
hi!

I'm trying to write a script which uses a form to get the user's first and last names has well has his favorite color, which are then stored in cookies and later used to change the page displayed, all pretty simple. My problem is that when I click on the button, an error message is displayed saying that there is an error on the first line, and I realy doubt there is an error in "<html>", so I was wondering if anyone whould be good enough to help me locade the
error(s).

Since I have no idea has of where the error lies, I'l have to display the whole script...

Thanks!

<html>
<head>

<title>Formulaires et témoins</title>

<script language="javascript">
<!--

function estVide(texte)
{
if(texte.length == 0)
{
alert("Veuillez remplir tous les champs.");
return true;
}
for (var i = 0; i <= texte.length-1; i++)
if (texte.charAt(i) != " ")
{
return false;
}
alert("Veuillez remplir tous les champs.");
return true;
}

function nomInvalide(nom)
{
if(estVide(nom))
{
return true;
}
if(nom.indexOf(" ", 0) != -1)
{
alert("Le prénom et le nom ne peuvent contenir d'espace. Si vous avez un nom composé, veuillez inscrire un trait d'union entre les mots.");
return true;
}
return false;
}

function couleurInvalide(couleur)
{
if (couleur.charAt(0) != "#")
{
alert("Une valeur hexadécimale doit débuter par le symbole '#'.");
return true;
}
if (couleur.length != "7")
{
alert("Veuillez inscrire une valeur hexadécimale.");
return true;
}
return false;
}

function setCookies()
{
prenom = document.forms[0].elements[0].value;
nom = document.forms[0].elements[1].value;
couleur = document.forms[0].elements[2].value;

var prenomCookie = "prenomCookie"+"="+ prenom +"; " + " path=/; " + "expires=Monday, 18-Dec-2006 00:00:01 GMT";
var nomCookie = "nomCookie"+"="+ nom +"; " + " path=/; " + "expires=Monday, 18-Dec-2006 00:00:01 GMT";
var couleurCookie = "couleurCookie"+"="+ couleur +"; " + " path=/; " + "expires=Monday, 18-Dec-2006 00:00:01 GMT";

window.document.cookie=prenomCookie;
window.document.cookie=nomCookie;
window.document.cookie=couleurCookie;
}

function validerForm(form)
{

if (estVide(form.prenom.value))
{
form.prenom.focus();
return false;
}
if (nomInvalide(form.prenom.value))
{
form.prenom.focus();
return false;
}
if (estVide(form.nom.value))
{
form.nom.focus();
return false;
}
if (nomInvalide(form.nom.value))
{
form.nom.focus();
return false;
}
if (estVide(form.couleur.value))
{
form.couleur.focus();
return false;
}
if (couleurInvalide(form.couleur.value))
{
form.couleur.focus();
return false;
}

setCookies();
writeToPage();

return true;
}

function writeToPage()
{
var cookieList = document.cookie;
var prenomCookieIndex = cookieList.indexOf("prenomCookie" + "=");
if (prenomCookieIndex == -1)
{
document.write("<center><H1>Témoins et formulaires</H1><p><form name='formulaire initial'>Veuillez inscrire votre prénom.<br><input type='text' value='Prénom' name='prenom'><p>Veuillez inscrire votre nom.<br><input type='text' value='Nom' name='nom'><p>Veuillez inscrire la valeur hexadécimale de votre couleur préférée.<br><input type='text' value='Couleur' name='couleur'><p><input type='button' value= 'Continuer' onClick='validerForm(this.form)'> <input type='reset' value='Effacer'><p></form></center>");
return true;
}

var sCookieValue = document.cookie;
prenomCookieValueStart = sCookieValue.indexOf("prenomCookie=") +13;
prenomCookieValueEnd = sCookieValue.indexOf(";" , prenomCookieValueStart);
prenomCookieValue = sCookieValue.substring(prenomCookieValueStart, prenomCookieValueEnd);
nomCookieValueStart = sCookieValue.indexOf(" nomCookie=") +11;
nomCookieValueEnd = sCookieValue.indexOf(";" , nomCookieValueStart);
nomCookieValue = sCookieValue.substring(nomCookieValueStart, nomCookieValueEnd);
couleurCookieValueStart = sCookieValue.indexOf("couleurCookie=") +14;
couleurCookieValueEnd = sCookieValue.length;
couleurCookieValue = sCookieValue.substring(couleurCookieValueStart, couleurCookieValueEnd);

aujouddhui = new Date();
heures = aujouddhui.getHours();
if(heures < 12)
{temps = ", bonne avant-midi."};
if(heures < 18)
{temps = ", bonne après-midi."};
else
{temps= ", bonsoir."};


document.write("<center><H1>Témoins et formulaires</H1><p>Bonjour, " + prenomCookieValue + " " + nomCookieValue + temps +"</p></center>");
document.bgColor = "couleurCookieValue";
return true;
}

// -->
</script>

</head>

<BODY bgcolor="#ffffff" onLoad = writeToPage()>


<NOSCRIPT>
<H1>Your browser does not support JavaScript.</H1>
</NOSCRIPT>

</body>
</html>

Logician
08-20-2006, 05:07 PM
hi!

I'm trying to write a script which uses a form to get the user's first and last names has well has his favorite color, which are then stored in cookies and later used to change the page displayed, all pretty simple. My problem is that when I click on the button, an error message is displayed saying that there is an error on the first line, and I realy doubt there is an error in "<html>", so I was wondering if anyone whould be good enough to help me locade the
error(s).

Since I have no idea has of where the error lies, I'l have to display the whole script...
I recommend you use Mozilla/FireFox for debugging, the JS console is more informative.
Remove the semicolons -

if(heures < 12)
{temps = ", bonne avant-midi."};
if(heures < 18)
{temps = ", bonne après-midi."};
else
{temps= ", bonsoir."};

akninshar
08-24-2006, 09:16 PM
hi!

I'm now using mozilla/firefox which does give much more precise error messages but I still can't find what's wrong with my script and I was wondering if anyone could point out whats wrong with it.

Here are the error messages I'm getting :
I'm told that the "writeToPage()" function is not defined and that one of my else conditions has a syntax error in it. Here is the said function, can anyone tell me what's wrong with it? (if you need to see the rest of the script, see the first post above).

thanks!

function writeToPage()
{
var cookieList = document.cookie;
var prenomCookieIndex = cookieList.indexOf("prenomCookie" + "=");
if (prenomCookieIndex == -1)
{
document.write("<center><H1>Témoins et formulaires</H1><p><form name='formulaire initial'>Veuillez inscrire votre prénom.<br><input type='text' value='Prénom' name='prenom'><p>Veuillez inscrire votre nom.<br><input type='text' value='Nom' name='nom'><p>Veuillez inscrire la valeur hexadécimale de votre couleur préférée.<br><input type='text' value='Couleur' name='couleur'><p><input type='button' value= 'Continuer' onClick='validerForm(this.form)'> <input type='reset' value='Effacer'><p></form></center>");
return true;
}

var sCookieValue = document.cookie;
prenomCookieValueStart = sCookieValue.indexOf("prenomCookie=") +13;
prenomCookieValueEnd = sCookieValue.indexOf(";" , prenomCookieValueStart);
prenomCookieValue = sCookieValue.substring(prenomCookieValueStart, prenomCookieValueEnd);
nomCookieValueStart = sCookieValue.indexOf(" nomCookie=") +11;
nomCookieValueEnd = sCookieValue.indexOf(";" , nomCookieValueStart);
nomCookieValue = sCookieValue.substring(nomCookieValueStart, nomCookieValueEnd);
couleurCookieValueStart = sCookieValue.indexOf("couleurCookie=") +14;
couleurCookieValueEnd = sCookieValue.length;
couleurCookieValue = sCookieValue.substring(couleurCookieValueStart, couleurCookieValueEnd);

aujouddhui = new Date();
heures = aujouddhui.getHours();
if(heures < 12)
{temps = ", bonne avant-midi."}
if(heures < 18)
{temps = ", bonne après-midi."}
else
{temps= ", bonsoir."}


document.write("<center><H1>Témoins et formulaires</H1><p>Bonjour, " + prenomCookieValue + " " + nomCookieValue + temps +"</p></center>");
document.bgColor = "couleurCookieValue";
return true;
}

Arty Effem
08-25-2006, 05:17 AM
hi!
Here are the error messages I'm getting :
I'm told that the "writeToPage()" function is not defined and that one of my else conditions has a syntax error in it.
Fixing the original syntax error should have cleared that message. However there is a much more serious problem, in that writetopage() calls document.write() after the page has loaded. This must not happen as it destroys the content of the page.

You must rewrite the entire script in such a way that document.write is not called onload, or at any time thereafter.

If you still have a problem, post your entire code again, this time within [code] tags.

akninshar
08-25-2006, 09:29 AM
so you are saying that calling a document.write after the page has been loaded will destroy the content of the page, but what if i am trying to replace the content of the original page with this new one, then whould it be ok or are you saying that this simply can't be done?

the point of this script is to change the apperence of the page according to the data entered by the user on his first visit but without calling two different html documents. can it be done?

EDIT: the original page is blank and is then changed onLoad depending on if the user has already been on the page or not, and all works fine till that point.
the problem seems to be if i try to change it again with an onClick.

anyways, i've now rewritten the script so that it only changes onLoad and it all works fine.