www.webdeveloper.com
Results 1 to 8 of 8

Thread: javascript cookies

  1. #1
    Join Date
    Apr 2003
    Location
    Liverpool, England, UK
    Posts
    133

    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

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,136
    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/javascrip...kieScript.html
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Apr 2003
    Location
    Liverpool, England, UK
    Posts
    133
    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

  4. #4
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,136
    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
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  5. #5
    Join Date
    Apr 2003
    Location
    Liverpool, England, UK
    Posts
    133
    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!

  6. #6
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,136
    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.

    Code:
    <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
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  7. #7
    Join Date
    Apr 2003
    Location
    Liverpool, England, UK
    Posts
    133
    Hmmm. Ok, thanks. I'll see how I get on.

    Appreciate your help.

    Cheers

  8. #8
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,136
    You are welcome...
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles