Click here to Skip to main content
15,889,648 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to create cookies with url,id,name using javascript and how to get saved cookie values( url,id,name) append each page(recently opened page) in my website
Posted
Comments
Rashmi Sutar 14-May-15 6:03am    
HI Prakash,

Do you want to create the cookie for the sites and sub sites? Or you have only single site.

Thanks,
F-ES Sitecore 14-May-15 6:17am    
google "read and write cookies from javascript"
Prakash J 14-May-15 6:42am    
i tried ....
F-ES Sitecore 14-May-15 6:44am    
Then post what you've tried so far and someone might be able to spot the issue.
Prakash J 14-May-15 6:38am    
yes i want to create single site @Rashmi Sutar

1 solution

Hi Prakash,

You can use below javascript.

````


// this fucntion is executed first when the page is loading.
_spBodyOnLoadFunctionNames.push("checkCookie");

function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}

function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}

function checkCookie() {
var user = getCookie("Vet");
if (user == true)//if user is not null.
{
//Do your stuff.
}

else if (user == null || user == '') {
var r = confirm("Your Question here");
setCookie("Vet", r, 365);
if (r == true) {
//do your stuff
}
else if (user == "false") {
//do your stuff
}
}



````
 
Share this answer
 
Comments
Prakash J 14-May-15 7:03am    
i did some thing like above @Rashmi Sutar
Prakash J 14-May-15 7:04am    
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + '=' + cvalue + ';path="/";' + expires;
}

function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i].trim();
if (c.indexOf(name) == 0)
return c.substring(name.length, c.length);
}
return "";
}

function checkHistory(targetId) {
var history = getCookie("history");
var htmlContent = '';

if (history != "") {
alert("welcome" + history);
var insert = true;
var sp = history.toString().split(",");
for (var i = sp.length - 1; i >= 0; i--) {

htmlContent += ''
+ sp.substring(sp.lastIndexOf('/')) + '
<br>';
if (sp == document.URL) {
insert = false;
}
document.getElementById(targetId).innerHTML = htmlContent;
}
if (insert) {
sp.push(document.URL);
}
setCookie("history", sp.toString(), 30);
} else {
var stack = new Array();
stack.push(document.URL);
setCookie("history", stack.toString(), 30);
}
}
Prakash J 14-May-15 7:08am    
how to use array push in my above cookies
Prakash J 14-May-15 7:09am    
how to get saved cookies in each page
Rashmi Sutar 14-May-15 7:16am    
It will directly run if I copy and paste it my side.or do i need to add something?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900