JavaScript reading cookie not working - javascript

I have been searching for reading cookies for last several hours and posted questions in this site but still no luck.
All I need to do is set cookie on one page and read cookie on other page. I have tried escape and unescape but no result.
Here is my code on first page where I am setting cookie
document.cookie = 'province=' +window.escape($(elem).text()) +'; expires=Fri, 3 Dec 2014 20:47:11 UTC; path=/';
And here I am reading it
function re() {
var a = get_cookie("province");
alert(a);
window.location = "lp.aspx?"+a;
}
function get_cookie(cookie_name) {
var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
if (results)
return (window.unescape(results[1]));
else
return null;
}
I have tried all the answers on stackoverflow but still looking for solution.
Again I need to set cookie on one page and read it on other page.

Try using the following
var testcookieValue = $.cookie("testcookie"); // read
$.cookie("testcookie", 1); // write
Hope it helps.

Related

How do I add a path to a cookie function?

I am having some trouble adding the path of "/" to my cookie, which is set with a function.
function WriteCookie(){
if(document.part1.pn1.value==""){
return;
}
var cookievalue=escape(document.part1.pn1.value) + ";";
document.cookie="partnumber1=" + cookievalue;
I'm sure it is a very simple answer, but I just can't seem to get it to work.
I have tried something like:
function WriteCookie(){
if(document.part1.pn1.value==""){
return;
}
var cookievalue=escape(document.part1.pn1.value) + ";";
document.cookie="partnumber1=" + cookievalue + path=/;
But clearly this is not correct.
This is the answer you need:
document.cookie = name+"="+value+expires+"; path=/";
If you set path=/ the cookie is available for the whole domain. Otherwise, your cookie is saved just for the current page you can't access it on another page.
For more info read- http://www.quirksmode.org/js/cookies.html - review the Domain and path section

Bookmarklet for set and read cookies

I need (for practice) to set a cookie via bookmarklet in website X, and read him with another bookmarklet from website Y.
For example, set a cookie named "user" with value of "Guy" in Google, and read this from YouTube.
I managed to set the cookie, but can't think of any idea how to read him from website b.
Thanks!
You need two bookmarklets, a getter and a setter.
You go to site X and use the getter bookmarklet to read the cookie and let the user copy it to his clipboard.
Then you go to site Y and use the setter. The setter will prompt the user for the bookmarklet and the user will then paste it into the prompt. The code will then set the cookie accordingly.
You can of course combine these two bookmarklets into a single getter/setter. The prompt will contain the current cookie for the page. The user can then choose to either copy the cookie and cancel (using it as a getter) or choose to to alter the cookie and click "OK" (using it as a setter).
I was looking for a way to share cookies of a specific website with a friend (reading them in my browser via bookmarklet and my friend setting them on his browser also via bookmarklet). Not quite what you asked for, but searching brought me here. This is my approach:
First there is a bookmarklet for exporting cookies. It will remove unnecessary white-spaces and encode your data in a base64 string for safe transport:
javascript:(
function(){
prompt("GET cookies encoded in base64", btoa(document.cookie.replace(/\s/ig, "")));
}
)
();
Then there is a second bookmarklet for importing all cookies encoded in the string. You can also set an optional lifetime here (thanks to https://www.quirksmode.org/js/cookies.html):
javascript:(
function(){
var inputstring = prompt("SET cookies decoded from base64");
var inputclean = atob(inputstring).replace(/\s/ig, "");
if (confirm("These cookies will be imported:\n\n" + inputclean.replace(/;/ig, "; "))) {
var days = prompt("Cookie lifetime in full days", "365");
var cookiearray = inputclean.split(";");
cookiearray.forEach(function(entry) {
var expires = "";
var split = entry.split("=");
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = split[0] + "=" + (split[1] || "") + expires + "; path=/";
});
}
}
)
();
Do not forget you have to run those on a specific website or tab. It does NOT export the entire collection of the cookies your browser is storing.
According to this StackOverflow, how to get cookies from a different domain with php and javascript you can't get cookies from another domain UNLESS you have access to it, as it would be a huge security flaw.

Understanding this go back logic

I saw this go back code in a website and I am trying to use it on my website. I am having trouble in understanding how the toURl and refUrl are being generated in it. Please guide about their structure. Thanks
var backtriggered = false;
setTimeout('goBack()',timer);
function goBack() {
if(backtriggered) return false;
backtriggered = true;
toURL=getWelcomeCookie('toURL');
refURL = getWelcomeCookie('refURL');
var reg = /(.*)\/sites\/(.*)\/(\d{4})\/(\d{2})\/(\d{2})\/(.*)\/?/;
if(reg.test(toURL) == false){
document.cookie="toURL"+ "=" +escape(toURL)+";path=/; domain=.mycsnippets.com; expires=Thu, 01-Jan-1900 00:00:01 GMT";
document.cookie="refURL"+ "=" +escape(toURL)+";path=/; domain=.mycsnippets.com; expires=Thu, 01-Jan-1900 00:00:01 GMT";
}
location.href=toURL;
}
document.cookie="toURL"+ "=" +escape(toURL)+";path=/; domain=.mycsnippets.com; expires=Thu, 01-Jan-1900 00:00:01 GMT";
This line puts a value in the brower's cookie store. For more information I think you should read this first: http://en.wikipedia.org/wiki/HTTP_cookie
There are two ways to set cookies: through server response, and through javascript.
What this code does is set cookies using javascript which say where the visitor has been and where he is going before setting the url to the target url (toURL). This way next page when it is called it knows to go back to the url which was set in the cookies as the previous url.

Javascript cookies and redirect

I need help changing this code so that the cookie only last through the session instead of 1 year forward. What changes do I need to make?
function createCookie(name,value,) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000*365));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
I tried to do this, but it doesn't seem to work. Cookie is created but doesn't disappear after session closes.
function createCookie(name,value) {
document.cookie = name+"="+value+"; path=/";
}
--- Update ---
I made some small changes to the code:
function createCookie(name,value,expires) {
var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
Now I was using Chrome, and it didn't work with the code I had and it doesn't work with this code either. But this code works in IE, Firefox and Opera. The cookie is deleted when the session is over, but not in Chrome...
Chrome since the version 19 had made a breakthrough change regarding the handling of session cookie. In order to improve the user experience the session cookie will not be removed.
If I understood correctly, since the option set in chrome settings say: "Continue where I left off", the session cookie never expires.
Please look at:
Chrome doesn't delete session cookies
If you are using Chrome or Firefox then set expires to 0, if you are using IE then leave out the expires parameter all together.

Save cookies with javascript how to specify domain?

I'm using this code to save cookies:
function saveCookie(name,value) {
var date = new Date();
date.setTime(date.getTime()+(60*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
document.cookie = name+"="+value+expires+"; path=/";
}
My problem is that it saves the cookie with domain "example.com" and I want to write them to ".example.com" so I can also read them from subdomains. This is easy to do with PHP but I don't know how to do it with javascript. How can I add a dot before the domain when I save the cookie?
You already have path in there, domain is specified in the same way.
To permit reading from other sub-domains, try:
'; path=/; domain=.'+window.location.host;

Categories