I have set a cookie1 from [.abcsite.com]. when no domain mentioned from API it sets a cookie1 by default to [www.abcsite.com]. Now, the cookie1 gets duplicated, it creates issues in updating the value. How to delete cookie1 with [www.abcsite.com] domain only when cookie1 [.abcsite.com] domain is present as well.
function setCookie(c_name, value, expiredays) {
var date = new Date();
date.setDate(date.getDate() + expiredays);
document.cookie = c_name + "=" + escape(value) + ";expires="+date.toGMTString() + ";path=/";
}
You can use this function to set a cookie and remove by :
setCookie(name, "", -1);
and the way to read cookie by:
function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=")
if (c_start != -1){
c_start = c_start + c_name.length + 1
c_end = document.cookie.indexOf(";", c_start)
if (c_end == -1)
c_end = document.cookie.length
return unescape(document.cookie.substring(c_start, c_end))
}
}
return ""
}
Related
I have trying to see if a cookie exists or not, Here is my getCookie method:
function getCookie(name)
{
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
and here is how I calling that method:
var cookie = getCookie("counter");
and here is my condition I tried:
if(cookie == '')
{
}
else
{
//Always goes here even when I clear my cookies and I know it does not exist.
}
My condition always goes into the else, not matter what, what am I doing wrong?
You can check the value of cookie if not undefined
if (typeof(cookie) === 'undefined'){
CONSOLE.LOG('no cookie');
} else {
CONSOLE.LOG(' cookie exist');
}
Please try following function to get cookie:
function getCookie(c_name){
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1){
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1){
c_value = null;
}
else {
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
Is it possible? I want to save the state of the radio.. like : enabled or disabled.
if(dis == "dis1"){document.getElementById(dis).disabled=true;}
This does not last till the code it completed.
Once it is disabled, I want it to be disabled till the code is completed!
Here is a really nice implementation
var createCookie = function(name, value, days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
else {
expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";
}
function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) {
c_end = document.cookie.length;
}
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
}
So in your case
createCookie("radio", "checked/notchecked", 30);
and then to read it
getCookie("radio") === 'checked' or !== 'checked'
Source: How do I create and read a value from cookie? There are also some other solutions mentioned there, make sure you check them.
Logically this seems to be correct. However, either the setCookie or getCookie functions simply aren't firing?
cookie.js
function setCookie(c_name,value,exdays) { var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays); var c_value=escape(value) +
((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value; }
function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
index.php
var newCookie = parseInt(getCookie("liked_count"));
if(newCookie != null && newCookie != ""){
newCookie += 1;
setCookie("liked_count",newCookie,5);
}else{
setCookie("liked_count",1,5);
}
No matter which side of the if statement it follows, no cookie is set regardless. From what I can tell there are no errors or warnings, so could it be that it cannot find the setCookie and getCookie function inside the my cookies.js file?
The cookies.js file successfully locates, so I'm at my wits end here.
<head>
<script type="text/javascript" src="assets/js/cookies.js"></script>
</head>
Any help would be much appreciated!
EDIT:
Oh sorry, this is embarrassing... It turns out that the cookie.js file was being cached and I had actually moved file location. It was that simple. Sorry for this waste of time!
The problem you're having is with your use of:
var newCookie = parseInt(getCookie("liked_count"));
MDN parseInt Documentation
parseInt returns NaN if it fails. Instead of the following line:
if(newCookie != null && newCookie != ""){
you should have
if(!isNaN(newCookie)) {
http://plnkr.co/edit/2Nj5pj
How can I detect client-side whether a user has just re-opened chrome with "Continue where I left off" enabled?
I would like modify client-side behavior based upon whether the user has just come from a related page. Re-opening the browser should not activate this behavior, but document.referrer is preserved.
You can use Cookies.
The cookies are stored in the client side and they can be seted and retrived via javascript (if the browser configuration allows it). Here you have examples for setting ang getting cookies with javascript.
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value; /* In this line you set the value */
}
function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "="); /* In this line you get the key */
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end)); /* In this line you get the value */
}
return c_value;
}
You can get more information here: http://www.w3schools.com/js/js_cookies.asp
I am trying to read cookie named as Pdf which has following value
http://engine.edocbuilder.com/include/fileDownload-aspx?p=xBfpz3UGPkWamLTDILo%2fWbFqh3FomdYuByiTwfB4RXN0sDN6tY%2fDfxJzzfPZblUl5aSBO3v96%2bJ6acwT7L5oi8tyMuGwKshYtGK%2bfhgiSfM%3d&s=4995b496-e735-4a26-9801-253a34ab0481
But when I read this cookie I get value in following format, can you please tell me the solution about it?
s=a4823a90%2D80e6%2D4006%2D909c%2D69d567f2d318&http%3A%2F%2Fengine%2Eedocbuilder%2Ecom%2Finclude%2FfileDownload%2Easpx%3Fp=%2BRj53ETGUI%2FeK7H8yo2Zj%2F6z9Ggmk4VEgmPEtoA2NPDhomzjaYvk2wkh0OZzWt8OtDcATY%2BknqGG8AuxddA6LWccWAfbQgtI0dlVkWevheg%3D
Following is the code to read cookie:
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
to read and write cookie use this function:
function setCookie(c_name, value, expiredays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString() + "; path=/");
}
function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1)
c_end = document.cookie.length;
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
}
The cookie contains URL encoded data. Use unescape to decode the string:
var cookie = "s=a4823a90%2D80e6%2D4006%2D909c%2D69d567f2d318&http%3A%2F%2Fengine%2Eedocbuilder%2Ecom%2Finclude%2FfileDownload%2Easpx%3Fp=%2BRj53ETGUI%2FeK7H8yo2Zj%2F6z9Ggmk4VEgmPEtoA2NPDhomzjaYvk2wkh0OZzWt8OtDcATY%2BknqGG8AuxddA6LWccWAfbQgtI0dlVkWevheg%3D"
// Find starting position of "http" and retrieve the URL from there
var unEscapedURL = unescape(cookie.substring(cookie.indexOf("http"), cookie.length));
​Here's a working fiddle.
Just use the native decodeURIComponent method to have the proper format back:
return decodeURIComponent(y);
Live test case.
The "problem" is in the code writing the cookie, it's written encoded for some reason.