Reading a Cookie give strange format - javascript

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.

Related

How to detect invalid Js cookie and remove it

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 ""
}

Javascript - Check if cookie exists

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;
}

How to change text values in confirmation box?

A cookie must be set and also confirmed.
However, I want to change the text of the confirmation box? Is there any way?
Like OK=Hello and Cancel=Reset in the example below:
HTML:
<body onload="checkCookie()"></body>
Javascript:
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;
}
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 checkCookie()
{
var username=getCookie("username");
if (username!=null && username!="")
{
var conf = confirm("Welcome again " + username);
if(conf==true)
{
return false;
}
else
{
alert("Your cookies have been reset!");
}
}
else
{
username=prompt("Please input your name below:","");
if (username!=null && username!="")
{
setCookie("username",username,365);
}
}
}
In addition, is there any way to be done like this:
When the entered username is set, being also reset while Cancel/Reset is clicked.
I wouldn't complain if somebody uses jQuery to do so though.
Thanks in advance. I will rep who helps me out :)
http://jsfiddle.net/V6LZE/
There are other options using some jQuery plugin, you can edit OK & Cancel button text. Helpful links:
http://www.codeproject.com/Questions/431829/How-to-change-button-text-of-Alert-message-using-j
Display Yes and No buttons instead of OK and Cancel in Confirm box?
jsfiddle.net/taditdash/PHX2Y/
Hope will help!

Increment browser cookie in Javascript? (setCookie, getCookie method)

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

Javascript: Cookie can not be read in all the HTML pages

I copied and used getCookie and setCookie from W3Schools(http://www.w3schools.com/js/js_cookies.asp). Here are the codes of get and set:
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;
}
I frist set cookie in prepareDrive.html page
setCookie("pathName",path,365);
setCookie("formatName",ifFormat,365);
Then I called get cookie in startInstall.html page which is a different HTML page
var path = getCookie("pathName");
var ifFormat = getCookie("formatName");
but both path and ifFormat are null. However, when I console.log in prepareDrive.html, the data is there. Thanks !!! This is my first time to use cookie in JS. I do not want to use localstorage to store data.Because some old version browsers do not support this function ,right?
You need to specify a common path for the cookie. Simplest is just to specify the domain root:
var c_value=escape(value) + "; path=/" + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
Without that, document.cookie will default to the current location.pathname, making the cookie only available to the current page.
;path=path (e.g., '/', '/mydir') If not specified, defaults to the current path of the current document location.
Also, I suggest having a look at the "little framework" for cookies on MDN.
If you are already using jquery you might want to consider the jquery plugin:
https://github.com/jquery/plugins.jquery.com
That makes it pretty straightforward.

Categories