Cookies/JavaScript

Qaybta Xirfada Sayniska iyo iwm

Moderators: Moderators, Junior Moderators

User avatar
YoUnG_Bl0o0d
Posts: 131
Joined: Fri Oct 22, 2004 6:49 am
Location: Birmingham

Cookies/JavaScript

Post by YoUnG_Bl0o0d »

For part of my degree course i have to design a website. We are marked mainly on the ideas and set up of the site, not really on how it looks. They way it starts is a person will go to the site and first go to the intro page which will mainly be a short flash animation.

What i would like to include is soma kind of code that places a cookie in the personas computer when they first visit the site, then when the person returns to the site i want it so there is some kind of code in my intro page that checks for that cookie, if the cookie is there then it redirects it to the 'main' page rather than loading the intro page up again.

I'm guessing the best or only way to achieve this would be through the use of a cookie and using javascript, so i was wondering...

1) is this even possible?
2) could someone either point me in the direction to were i can get the code or info i need to do this or even provide me with the code i need? I tried google but didn't get much luck, wasn't sure what i was even looking for or if what i wanted to do was possible
3) any other ideas of demonstrating the use of javascript is welcome

Thanks alot
smooth
SomaliNet Heavyweight
SomaliNet Heavyweight
Posts: 3947
Joined: Tue Jan 20, 2004 7:00 pm
Location: London

Post by smooth »

yes its possible a good resource for all Javascrip issues is located at their website, the actuall code you want is located her

http://javascript.internet.com/cookies/ ... irect.html

dont just cut and paste the code's, make sure you spend a few minutes reading through it, have fun sxb
User avatar
YoUnG_Bl0o0d
Posts: 131
Joined: Fri Oct 22, 2004 6:49 am
Location: Birmingham

Post by YoUnG_Bl0o0d »

Smooth i re-wrote the code's myself but still after hours of carefully going through them i still cant get it to work, her it is

function ReDirect (URL) {
GetCookie('SplashSkip');
if (Splash == 'TRUE') {
window.location=(URL);
}
else {}
}
var Splash = GetCookie('SplashSkip');
</head>

<body>
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (';', offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
var arg = name + '=';
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(' ', i) + 1;
if (i == 0) break;
}
return null;
}
ReDirect('mainpage.htm);
function SetCookie (name, value) {
// Enter number of days the cookie should persist
var expDays = 30;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays * 24 * 60 * 60 * 1000));
expirationDate = exp.toGMTString();
// Set cookie with name and value provided
// in function call and date from above
document.cookie = name + '=' + escape(value)
document.cookie += '; expires=' + exp.toGMTString();
}
//--> this is LINE 53!!!
// -->
</script>

<body onload="SetCookie('SplashSkip','TRUE');">
<div align="center"><a href="pop.htm">this is a link</a></div>
</body>
</html>


all i know is there is an error on line 53 why i have said in the code line number 53 is to save you counting, in actual fact i left it blank. From what i can gather it is expecting an object to be on that line, most likley my flash animation? i was wondering if that was the reason why it wasn't working right now because i havn't inserted the flash object into the site yet? could that be the case.? i'm struggling could any 1 help
smooth
SomaliNet Heavyweight
SomaliNet Heavyweight
Posts: 3947
Joined: Tue Jan 20, 2004 7:00 pm
Location: London

Post by smooth »

Young.

The major problem you got is that you dont understand the debug process
The debug is showing where the script engine found the error line 53. When the script is ran in blank lines they are not processed therefore in your code line 53 its actualy the line above, It can sometimes be hard to find an error expecially if you dont understand the debug process, her is the exact code you should have had

<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
function ReDirect (URL) {
GetCookie('SplashSkip');
if (Splash == 'TRUE') {
window.location=(URL);
}
else {}
}
var Splash = GetCookie('SplashSkip');

function getCookieVal (offset) {
var endstr = document.cookie.indexOf (';', offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
var arg = name + '=';
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(' ', i) + 1;
if (i == 0) break;
}
return null;
}
ReDirect(INESERT THE URL TO THE PAGE you WANT TO REDIRECT TO HERE);
function SetCookie (name, value) {
// Enter number of days the cookie should persist
var expDays = 30;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays * 24 * 60 * 60 * 1000));
expirationDate = exp.toGMTString();
// Set cookie with name and value provided
// in function call and date from above
document.cookie = name + '=' + escape(value)
document.cookie += '; expires=' + exp.toGMTString();
}
// -->
</script>

<body onload="SetCookie('SplashSkip','TRUE');">
</head>
<body>
Insert your body here
</body>
</html>
User avatar
Mrs Junior Cappa Donna
Posts: 125
Joined: Wed Aug 10, 2005 1:25 am

Post by Mrs Junior Cappa Donna »

Yep.. Smile
Locked
  • Similar Topics
    Replies
    Views
    Last post

Return to “Careers - Engineering, Science & Computers”