Hello, I am trying to set a cookie and then read it on another page . The problem is I don't know how to do it But I found a code at htmlgoodies.com I think in the javascript tutorial . Now when I place both pages in same directory , it works fine . But when the pages are placed in different directories , the cookie is not read . Is there a way to read this cookie in another page in a seperate directory ? The code I am using is as follows . "Page that sets cookie" . <head> <SCRIPT LANGUAGE="JavaScript"> cookie_name = "dataCookie"; var YouEntered; function putCookie() { if(document.cookie != document.cookie) {index = document.cookie.indexOf(cookie_name);} else { index = -1;} if (index == -1) { YouEntered=document.cf.cfd.value; document.cookie=cookie_name+"="+YouEntered+"; expires=Monday, 04-Apr-2010 05:00:00 GMT"; } } </SCRIPT> </head> <body> <FORM NAME="cf" action="submit"> <INPUT TYPE="hidden" NAME="cfd" value="username"> <INPUT TYPE="button" Value="Click here to contact us " onClick="putCookie(); parent.location='../retrieve.html'"> </FORM> </body></html> [size=+1]" Then in the file named "retrieve.html" in another "directory" ."[/size] <head> <SCRIPT LANGUAGE="JavaScript"> cookie_name = "dataCookie"; var YouWrote; function getName() { if(document.cookie) { index = document.cookie.indexOf(cookie_name); if (index != -1) { namestart = (document.cookie.indexOf("=", index) + 1); nameend = document.cookie.indexOf(";", index); if (nameend == -1) {nameend = document.cookie.length;} YouWrote = document.cookie.substring(namestart, nameend); return YouWrote; } } } YouWrote=getName(); if (YouWrote == "dataCookie") {YouWrote = "Nothing_Entered"} </SCRIPT> </head> <body> <SCRIPT LANGUAGE="javascript"> document.write("<INPUT TYPE=text name=sendername SIZE=30 VALUE=" +YouWrote+ ">"); </SCRIPT> If I place both pages in same directory , it reads the cookie but when I place them in different directories , it doesn't. Is there a way ? Kindly modify the line that needs to be changed in the above code . Thanks Regards Jeet
I'm really bad with javascript, but had a similar problem some time. When you set the cookie with document.cookie=cookie_name+"="+YouEntered+"; expires=Monday, 04-Apr-2010 05:00:00 GMT"; you can also specify if the cookie will work for the whole domain or just for subfolders (relative to the page is setting the cookie). Play around with the following, should work: document.cookie=cookie_name+"="+YouEntered+"; expires=Monday, 04-Apr-2010 05:00:00 GMT"; path="/""; domain="insert-your-domain.name.tld""; I'm, not really sure about the quotes, but mainly the problem is this. I hpoe this tip helps you. Edited: just to remove the quote, made it so long...
You could try this maybe: document.cookie=cookie_name+'='+'Test Message'; expires="Monday, 04-Apr-2010 05:00:00 GMT"; path='/'; domain='yourdomain.com'; and remember to change your domain on the above. But if you can provide alink to the test page it will be easier to help.
Hi, There is a cookie but it is not being read . Another thing I was thinking about is : If I could name the cookie like "cookie:ad4business.com" because the domain is http://www.ad4business.com , and then read the cookie by the name , will that work ? When I look in my internet folder , I see cookies by similar names with the name of the domain like "cookie:computername@google.co" , digital point etc. Which variable should I change to do this ? This way I need not specify the path but simply read a "specific" name .Will that be easier ? Thanks Regards Jeet
No, it won't. If you please post (or PM) the test page maybe I could help. Also, I think the code needs to completely rewritten (but that's just my way of working, it symply looks confusing to me).
Hi, The page that holds the first part is at http://www.ad4business.com/freehost or http://www.ad4business.com/freehost/index.html The page that is supposed to read the cookie is at http://www.ad4business.com/contact1.html The code above works fine if both pages are in the same directory like : http://www.ad4business.com/index.html http://www.ad4business.com/contact1.html However the page that reads the cookie is in a higher level directory which creates the problem . In the page at http://www.ad4business.com/contact1.html you will see a field like "referred by " . This should "not" show "undefined" if you are coming from the page at http://www.ad4business.com/freehost/index.html The "contact button" there sets the cookie . Regards jeet
Hello, Looks like this code works : document.cookie=cookie_name+"="+YouEntered+"; expires=Monday, 04-Apr-2010 05:00:00 GMT; path=/" Thanks Jeet
Happy it worked. I shuold say that I can't hardly see what you are trying to do, but if it works that's all.
Hello, The code above is picking a value from a form field . For example the login form on DP ... It then writes this value to a cookie when "submit " is clicked . The next page picks the value and fills in the respective field on this current page . This is very similar to passing information in a shopping cart system . Although that is a little more complicated . Thanks for your help and the idea of adding PATH . Regards Jeet