Click to See Complete Forum and Search --> : javascript cookies
bloke
06-04-2003, 09:50 AM
Hello!
My ignorance now turns to javascript!
Ok, here goes. I currently use js to open a window thus:
<script language="javascript">
function openFile(jsVar)
{
window.open(jsVar, "File", "toolbars=no, scrollbars=yes, height=600, width=450, resizable=yes, left=250, top=70");
}
</script>
<tr bgcolor="#ffffff" onmouseover="this.bgColor = '#C0C0C0';window.status=''" onmouseout="this.bgColor = '#ffffff'">
<td class="ContentDetail">
<a href="javascript:;" class="ContentDetail" onClick="openFile('<%=strURL%>');"><%=rscorres("document_name")%></a>
</td>
</tr>
Which is fine. However, what I need to do is when the user clicks on this link to open the window, a cookie is also set with a variable name and a variable value.
To cut a long story short (too late), I need to indicate that a user has read a particular document or not.
Any help will be muchly appreciated.
Regards
Khalid Ali
06-04-2003, 09:59 AM
To do something like that..
1.When a link is clicked..first read cookie to see if this particular value is set already?
2. if not then set a new cookie3. else do not open the document or open the doc - depends on what you want to do...
Here is a simple cookie script...let me know if you are not able to implement it in your situation....
http://68.145.35.86/skills/javascripts/SimpleLoginCookieScript.html
bloke
06-04-2003, 10:04 AM
Woah!!
Just looked at the source of your page - bit over my head to be honest. Javascript aint my strongest suit.
I need to display an icon in a results page depending on whether or not the user has already read the document.
Any chance you could wizz out the relevant bits of code for me?!
Cheers
Khalid Ali
06-04-2003, 10:11 AM
I guess I'll be able to put some together for you..
Post the actual webpages link here...so that I can see your real code and structure and insert the required pieces
bloke
06-04-2003, 10:28 AM
Intranet I'm afraid. I want the cookie name to contain the user id and document id (eg BobH4321 - made up of userid BobH & document id 4321) and I guess the value can just be true or 1 or something.
I was thinking something along the lines of:
<script language="javascript">
function openFile(jsVar)
{
document.cookie["userid+docid" = "true"; expires = "December 31, 2010";]
window.open(jsVar, "File", "toolbars=no, scrollbars=yes, height=600, width=450, resizable=yes, left=250, top=70");
}
</script>
<tr>
<td class="ContentDetail"> <a href="javascript:;" class="ContentDetail" onClick="openFile('<%=strURL%>');"><img src="../images/magnify.gif" border="0" alt="click here to view this document"></a> </td>
<td align="center">
<%If request.cookies("userid+docid") = true Then%>
<img src="../images/envread.gif" width="16" height="11" alt="" border="0">
<%Else%>
<img src="../images/envunread.gif" width="16" height="11" alt="" border="0">
<%End If%>
</td>
</tr>
...but not sure how I could pass the variables into the cookie.
Or am I talking complete nonsense!! Ha ha!
Khalid Ali
06-04-2003, 11:50 AM
use the following javascript and get rid of the one you already have..(unless there is some more which is not shown in this thread by you.
<script type="text/javascript">
<!--
var expireIn = 60 * 1000;
var expire = new Date();
var cookieName = "fileName";
function openFile(jsVar){
var userId = "BobH";
var docId = "4321";
var storedFile = GetCookie(cookieName);
if(storedFile!=(userId+docId)){
CreateCookie(userId+docId);
window.open(jsVar, "File", "toolbars=no, scrollbars=yes, height=600, width=450, resizable=yes, left=250, top=70");
}else{
alert("error message that file is already created");
}
}
/*
@param val Value to be put in cookie
*/
function CreateCookie (val) {
//want this cookie to expire in 30 days
//timesVisited +=1;
var thisVisit = new Date();
expire.setTime (expireIn + parseInt(expire.getTime())); //1 mins from now!
var WholeCookie = val;
document.cookie = cookieName+"=" + escape (WholeCookie) + "; expires=" + expire.toGMTString();
}
/*
@param CookieName is passed to get Cookie method to read a cookie
@return returns an array of values in the cookie separated by a ` character.
*/
function GetCookie (CookieName) {
var cname = CookieName + "=";
var i = 0;
while (i < document.cookie.length) {
var j = i + cname.length;
if (document.cookie.substring(i, j) == cname){
var leng = document.cookie.indexOf (";", j);
if (leng == -1) {
leng = document.cookie.length;
}
return (unescape(document.cookie.substring(j, leng))).split("`");
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
}
//-->
</script>
This above will expire in 1 minute
take a look at the top at this line
var expireIn = 60 * 1000;
you will need to change it to the expiration time frame as you need.
You may want to read up at description at this link.
http://wp.netscape.com/newsref/std/cookie_spec.html
bloke
06-05-2003, 03:07 AM
Hmmm. Ok, thanks. I'll see how I get on.
Appreciate your help.
Cheers
Khalid Ali
06-05-2003, 08:38 AM
You are welcome...