...

View Full Version : Hello, i remember you!



wylie233
07-23-2012, 12:12 PM
Hello,

I am tring to use a cookie, the idea is, when some one types into a textfield:

"my name is xxxxx", "hello xxxxxx" is returned, and when the window is closed,
and opened "hello again, xxxxx" is returned, I have made:



<html>
<head>


<script type = "text/javascript">

var user_name;
var expires = new Date();


if ((document.cookie == "") == false) {

var length = document.cookie.length - 1;
var message = document.cookie.substr(5,length);

document.write("<h2><center>Welcome back, " +document.cookie + "!</center></h2>");

}

function check() {

user_name = document.getElementById("name").value;
expires.setFullYear(expires.getFullYear() + 1);

document.cookie = escape("name") + "=" + escape(user_name) + "; expires = " + expires.toGMTString();

alert(document.cookie);


}












</script>
</head>
<body>





Name: <input type = "text" id = "name" />

<input type = "button" value = "Enter" onclick = "check()" />




</body>
</html>







I do not need the alert, that was part of the tutorial.



Thank you!

vwphillips
07-23-2012, 12:35 PM
<html>
<head>


</head>
<body>


<script type = "text/javascript">

var days = 1; // the number of days to remember the name


if (cookie('name')) {
document.write("<h2><center>Welcome back, " +cookie('name') + "!</center></h2>");
}

function check() {
var nme = document.getElementById("name").value;
if (nme){
document.cookie='name='+nme+';expires='+(new Date(new Date().getTime()+days*86400000).toGMTString())+';path=/';
}
}

function cookie(nme){
var re=new RegExp(nme+'[^;]+','i');
if (document.cookie.match(re)){
return document.cookie.match(re)[0].split("=")[1];
}
return null
}

</script>



Name: <input type = "text" id = "name" />

<input type = "button" value = "Enter" onclick = "check()" />




</body>
</html>

Philip M
07-23-2012, 01:10 PM
Vic's script is fine but here is my improved version - check for invalid names, avoid document.write(), hide input textbox when not required etc.


<html><body>

<head>

</head>

<span id = "message"></span>
<div id = "inp" style="display:block">
Enter Your Name: <input type = "text" id = "name" onblur = "check()" />
</div>

<script type = "text/javascript">

var days = 1; // the number of days to remember the name

if (cookie('name')) {
document.getElementById("message").innerHTML = "<h2><center>Welcome back, " +cookie('name') + "!</center></h2>"
document.getElementById("inp").style.display="none";
}

function check() {
var nme = document.getElementById("name").value;
nme = nme.replace(/[^a-z\-\s\']/gi,""); // strip invalid characters
nme = nme.replace(/^\s+|\s+$/g,""); // strip leading and trailing spaces
nme = nme.replace(/\s{2,}/g," "); // replace multiple spaces with one space
nme = nme.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()}); // capitalise initial letter of each word
if (nme.length > 1) { // at least two valid characters left
document.getElementById("message").innerHTML = "<h2><center>Welcome " + nme + "!</center></h2>"
document.getElementById("inp").style.display="none";
document.cookie='name='+nme+';expires='+(new Date(new Date().getTime()+days*86400000).toGMTString())+';path=/';
}
}

function cookie(nme){
var re=new RegExp(nme+'[^;]+','i');
if (document.cookie.match(re)){
return document.cookie.match(re)[0].split("=")[1];
}
return null
}

</script>

</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum