How to get cookies value on another page with jQuery? - javascript

I have called one PHP page inside an iframe
I have stored some values in cookies. I want to read those cookies values from that page to other page.
I used jQuery to read the cookie.
var value = $.cookie('artname');
'artname' is the cookie name.
But it is displaying null because the cookie path is different. The path is /v/abcfile/frontend/.
But the path for the other cookies on the page i am trying to get is /.
I tried with this:
top.jQuery.cookie('artname');
But it's still showing me the same.
For path ii have tried with:
var value = $.cookie("artname", { path:'/v/vspfiles/frontend/' });
Its still showing me the null value.
How can I get value of cookie?

When you save your cookies, set path to "/". Cookie will be available on all pages
$.cookie('artname', 'value', { path:'/'});

This worked for me:
$.cookie.defaults = { path: '/' };
$.cookie("foo", "value" );

Related

how to store path name value in local storage or cookie

I have angular app and based on previous path name not page name i want to store the value either in cookie or local storage
Path name of first page is /content/drive.html/find-drive
Path name of second page is /content/drive.html/drive-results
When I am on the second page I want to store find-drive value in cookie
Similarly
Path name of first page is /content/drive.html/find-drive-advanced
Path name of second page is /content/drive.html/drive-results
When I am on second page i want to store the previous path location in my cookie or local storage.for this scenario i want to store"find-drive-advanced"
Note: document.referrer doesn't work, and window.parent.URl doesn't work
Use localStorage
Say that url1 = '/content/drive.html/find-drive', and url2 = '/content/drive.html/drive-results'
just save them like so:
localStorage.setItem('url1', url1);
localStorage.setItem('url2', url2);
to retrieve the value, just write
var url1 = localStorage.getItem('url1');

document.cookie returns only PHPSESSID=*random number*

I'm trying to get a cookie I have set but all I'm getting is the PHPSESSID.
I set my cookie in a separate PHP page with:
setcookie("username", $sentname, time()+(60*60*24*30),NULL,NULL,NULL,false);
I can then find it through firefox settings.
When I try to access it using JavaScript on a different page I use:
<script>
$(document).ready(function(){
var cookie = document.cookie;
alert(cookie);
});
</script>
which then returns:
PHPSESSID=gvjsgfd8etlbdq43lndni3o0g4
It should return all the cookies, only "username" so far, I have set in a key paired string. I tried using the jquery plugin for cookies and it returned the same thing. I also couldn't find this problem elsewhere online.
Not sure if I should delete the question but one of the related links gave me the answer.
The cookie path must be set to '/' to be accessible from all subdomains. so I changed it too:
setcookie("username", $sentname, time()+(60*60*24*30),'/',NULL,NULL,false);
which gives me:
PHPSESSID=gvjsgfd8etlbdq43lndni3o0g4; username=asdf

jQuery Cookie returns [object Object]

I've got these two functions:-
function BlogsButtons() {
$.removeCookie('BlogURLs', { expires: 7, path: '/en/blogs/' });
var urls = "";
$("#list-of-blogs div.news-article").each(function() {
urls += $(this).find('a:first-child').attr('href');
if(!$(this).is(":last-child")) {
urls += ",";
}
});
$.cookie('BlogURLs', urls, { expires: 7, path: '/en/blogs/' });
var a = 1;
}
and
function BlogsNextLastButtons() {
var blogsURLs = $.cookie('BlogURLs', { expires: 7, path: '/en/blogs/' });
// .. to do rest of function here
}
I have a page which lists blogs, and I want to get the list of URL's and comma separate them and store them in a cookie. Then on the page that has the actual article on, I want to retrieve this list from the cookie, split it by comma and use basic array surfing to find the item in the list before the current page URL and then item after. The logic for this works.
In the second function on that first line, when I try to get back the value of $.cookie('BlogURLs'), the console shows me [object Object]. I've read on another post that this can be caused by a cookie being overwritten, but this is definitely the only place on the site where a cookie with such a name is used.
Also, interesting to note is that the line of code var a = 1, I can see the cookie value here in the correct format. So whatever causes this happens after the page with the actual article on loads up.
Stuff I've tried:-
Renaming the cookie name to something absurd that can't possibly be used.
Changing the path of the cookie
Instead of doing removeCookie, I tried setting the cookie to null, empty string and an expiry time of -1.
Nothing seems to work. In an exact duplicate of this function above, we have no issues, the cookie name there is different again, as is the path.
Can anyone help?
Thanks.

How to detect if cookie is set in Javascript ?

I have set a session in PHP, which is creating a cookie: PHPSESSID...
I can detect this in Chrome & Opera by using document.cookie. However in Forefox, document.cookie also returns cookies set on the page by other domains, e.g. Google Analytics.
In PHP I am setting the sessions like:
session_start();
$_SESSION['source'] = &$ref['source'];
$_SESSION['term'] = &$ref['term'];
session_write_close();
I need to be able to detect if this session is set in Javascript, by locating the cookie. What is the best way to go about this?
At the moment I am just using:
document.cookie.indexOf( 'PHPSESSID' )
which seems like a bit of a botch.
The document.cookie property will return all the cookies. While your indexOf will work, it will break if your cookies actual data contains 'PHPSESSID'. It will also match the following cookie 'MYPHPSESSIDIDIT', as it contains your cookie name.
You could parse the cookies with the following function (not tested):
function getCookieValue(name)
{
// find cookie entry in middle?
var s=document.cookie,
c=s.indexOf("; "+name+"=");
if(c==-1)
{
// no, is it at the start?
c=s.indexOf(name+"=");
if(c!=0) return null;
}
// get length of value
var l=c+name.length+1,
e=s.indexOf(";",l);
// is it at the end?
if(e==-1) e-s.length;
// cut out the value
return s.substring(l,e);
}
Hope this helps
Use this Jquery plugin, it's so cool.
https://github.com/carhartl/jquery-cookie
You can use it like this way:
if($.cookie('PHPSESSID') != undefined){
//PHPSESSID exists
}

How to get cookie value with javascript or jquery

I am getting following cookie values on my home page.
vsettings=HiddenNotifications=%2C19%2C;
(referral)|utmcmd=referral|utmcct=/;
ASPSESSIONIDQCSBCCAD=LFLGDKACEGPCPFGBANHFFBMK;
ASPSESSIONIDSASDCCDA=OFEHPJACGHNKICDLFBOCJNNH;
ASPSESSIONIDQCSDDCBC=LGFPBDLCKHLIOLPMCAGJNFIM;
ASPSESSIONIDQCTACBAC=DCIPJDLCNDBEGEIIELCGFMDP;CustomerID;
ASPSESSIONIDSCTCAABD=LHPFFLFDEMIBIENBMGMLFIFP;
ASPSESSIONIDSAQDDDAD=HMCDMLFDFCLJCIBHMEHPNHME; s_cc=true;
__utmc=97358351; CustomerID=755B031ED8C016EC4F90D6F3127E776AE202DEF5BC6EB9F7A9BB19E49323BC3B;
ASPSESSIONIDQCSDBDAC=CFBDJEAAPJJGHMDGOCCHALHC;
ASPSESSIONIDSASCBDAD=FLGJHFAAJAJBICGIMOOJEFFF;Session%5FToken=C8F2A63C3BBA4E39BBF73B83C108D1C6
I want get the value of CustomerID cookie.
Using jquery I wrote this:
$.cookie('CustomerID',{ path:'/'});
But I am not getting the value for cookie - it is showing me blank.
How can I get the CustomerId cookie?
What you did is setting a cookie and not getting a cookie.
try var customerId = $.cookie('CustomerID');
or maybe you are trying to get the cookie from the root path but it's actually lives at a lower path..
Check out where you save the cookie!

Categories