/    Sign up×
Community /Pin to ProfileBookmark

javascript cookies

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

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliJun 04.2003 — 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
Copy linkTweet thisAlerts:
@blokeauthorJun 04.2003 — 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
Copy linkTweet thisAlerts:
@Khalid_AliJun 04.2003 — 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
Copy linkTweet thisAlerts:
@blokeauthorJun 04.2003 — 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!
Copy linkTweet thisAlerts:
@Khalid_AliJun 04.2003 — 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.

<i>
</i>&lt;script type="text/javascript"&gt;
&lt;!--
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 <span><code> character.
*/
function GetCookie (CookieName) {
var cname = CookieName + &quot;=&quot;;
var i = 0;

while (i &amp;lt; document.cookie.length) {
var j = i + cname.length;
if (document.cookie.substring(i, j) == cname){
var leng = document.cookie.indexOf (&quot;;&quot;, j);
if (leng == -1) {
leng = document.cookie.length;
}
return (unescape(document.cookie.substring(j, leng))).split(&quot;</code></span>");
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
}
//--&gt;
&lt;/script&gt;


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
Copy linkTweet thisAlerts:
@blokeauthorJun 05.2003 — Hmmm. Ok, thanks. I'll see how I get on.

Appreciate your help.

Cheers
Copy linkTweet thisAlerts:
@Khalid_AliJun 05.2003 — You are welcome...
×

Success!

Help @bloke spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...