PDA

View Full Version : Please Help With Window.location



homboy
03-28-2008, 12:42 AM
Hi, guys i really need help with one thing
i am trying on my index page to use a form with a cookie so that when they enter they username on the index page, it uses window.location to reload the index page with their name

i know its simple but i cant figure it out
please help

rangana
03-28-2008, 01:08 AM
This link (http://www.trans4mind.com/personal_development/JavaScript/cookie4.htm) should keep you moving ;)

Nile
03-28-2008, 01:20 AM
Rangana, I think he wants an input type.

rangana
03-28-2008, 01:28 AM
Something like this ;)


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title></title>
<script type="text/javascript">
function myCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function readmyCookie(name) {
var nameEquiv = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEquiv) == 0) return c.substring(nameEquiv.length,c.length);
}
return null;
}
window.onload = function()
{
if(readmyCookie('username') != null)
{
document.getElementById('name').firstChild.nodeValue = readmyCookie('username');
}
}
</script>
</head>

<body>
<span id="name">&nbsp;</span>
<form action="" method="post">
<input type="text" name="myusername">
<input type="submit" value="Submit" onclick="myCookie('username',this.form.myusername.value,800);">
</form>
</body>
</html>

Nile
03-28-2008, 01:43 AM
A little edit to your code Ragana, but if mine suits best for you, thanks Ragana:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title></title>
<script type="text/javascript">
function myCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function readmyCookie(name) {
var nameEquiv = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEquiv) == 0) return c.substring(nameEquiv.length,c.length);
}
return null;
}
window.onload = function()
{
if(readmyCookie('username') != null)
{
document.getElementById('name').firstChild.nodeValue = readmyCookie('username');
document.getElementById('n').innerHTML="";
}
}
</script>
</head>

<body>
<span id="name">&nbsp;</span>
<form action="" method="post"><span id="n">
<input type="text" name="myusername" onChange="myCookie('username',this.form.myusername.value,800);"></span>
</form>
</body>
</html>

homboy
03-28-2008, 02:39 AM
Hi thanks for your help
every time i open this on internet explorer. it says "to help protect your security, internet explorer has restricted this webpage from running scripts or activex that could access your computer." is there anyway i can do this without that message coming up
and also how do i set a greeting saying welcome "username"
and is there anyway i can use window.location to reload the page
thanks

rangana
03-28-2008, 02:52 AM
That warning does'nt happen when the page is in the server..only on local machine ;)

Add this code in the onload event? ;)


document.getElementById("myusername").style.display="none";


window.location is the same as document.location (maybe) which automaticallyt reloads the page :)

Make sure you had cleared your cache already :)

homboy
03-28-2008, 02:59 AM
thanks for your help
but the submit still stays after i added the new code
any reason why this is so?
also how do i add a greeting that says welcome "user"
thanks

rangana
03-28-2008, 06:00 AM
Maybe this will aide ;)


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<style type="text/css">
#name
{
border:3px double #222;
width:760px;
}
</style>
<script type="text/javascript">
function createCookie(name,value,days)
{
if(days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i = 0;i < ca.length;i++)
{
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1,c.length);
if(c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
window.onload = function()
{
if(readCookie('username') != null)
{
document.getElementById('name').firstChild.nodeValue = "Welcome back, " + readCookie('username');
document.getElementById("nameinputform").style.display="none";
}
else
{
document.getElementById('name').firstChild.nodeValue = "Welcome, Guest!";
}
}
</script>
</head>
<body>
<span id="name">&nbsp;</span>
<form action="" method="post" id="nameinputform" onsubmit="createCookie('username',this.uname.value,888);">
<label for="uname">Please Enter Your Name:
<input type="text" name="uname" id="uname">
</label>
<input type="submit" value="Send">
</form>
</body>
</html>

homboy
03-28-2008, 04:12 PM
hey guys thanks for all your help
I was also wondering how can i make the cookie expire after the browser closes
here it doesnt seem to expire

homboy
03-28-2008, 10:46 PM
hey guys thanks for all your help
I was also wondering how can i make the cookie expire after the browser closes
here it doesnt seem to expire
and also if i wanted to use this cookie on a different page on my website what part of the code do i need on that other webpage
thanks a lot

Nile
03-28-2008, 10:51 PM
Here:


<form action="" method="post" id="nameinputform" onsubmit="createCookie('username',this.uname.value,888);">

Find that, then replace 888 with the amount of days you want it to stay there for, so I would just put 1 day.

homboy
03-28-2008, 10:59 PM
and also if i wanted to use this cookie on a different page on my website what part of the code do i need on that other webpage
thanks a lot

Nile
03-28-2008, 11:01 PM
This part:

function readmyCookie(name) {
var nameEquiv = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEquiv) == 0) return c.substring(nameEquiv.length,c.length);
}
And then since your cookie name is username, you'd use:


document.write(readmyCookie('username'));

Hope this helps. ;)

homboy
03-28-2008, 11:26 PM
Nile thanks for all your help
I was wondering you know all the functions on the 1st page, is there anyway i can put them in a javascript file instead of putting them all at "head"
thanks

Nile
03-28-2008, 11:42 PM
Yes there is, search google for internal and external javascript.

homboy
03-29-2008, 12:21 AM
This part:

function readmyCookie(name) {
var nameEquiv = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEquiv) == 0) return c.substring(nameEquiv.length,c.length);
}
And then since your cookie name is username, you'd use:


document.write(readmyCookie('username'));

Hope this helps. ;)

HEY I PUT THE FUNCTION READMYCOOKIE IN THE HEAD
WHERE DOES THE document.write go in the page?

Nile
03-30-2008, 02:13 AM
Just put it at the top of the script.

homboy
03-30-2008, 02:18 AM
is the full code client side or server side
thanks

Nile
03-30-2008, 02:20 AM
Client side, right after:


<script type="text/javascript">

Or right before:


</script>

homboy
03-30-2008, 03:02 AM
thanks for your help nile
i had just 1 more question
how can i create a cookie that will have a counter that will say how many people have visited my website
like a counter that will keep counting even after the browser is closed

Nile
03-30-2008, 03:05 AM
Can you use PHP?

homboy
03-30-2008, 03:44 AM
No i cant

Nile
03-30-2008, 12:49 PM
Search google for `Writing to files with javascript`.