So what I'm saying is that can you create a cookie without using JQuery and only JavaScript.
Do you have an answer?
I have these simple function to do that
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
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,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
Now, calling functions
createCookie('ppkcookie','testcookie',7);
var x = readCookie('ppkcookie')
Create cookies with javascript
JavaScript can create, read, and delete cookies with the document.cookie property.
With JavaScript, a cookie can be created like this:
document.cookie = "username=John Doe";
You can also add an expiry date (in UTC time). By default, the cookie is deleted when the browser is closed:
document.cookie="username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC";
With a path parameter, you can tell the browser what path the cookie belongs to. By default, the cookie belongs to the current page.
document.cookie="username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
All together code:
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
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("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 365);
}
}
}
Related
So what im tryin to do is to check for a cookie existance (for example accepted=yes) If it is not set, it will return nothing and if not, it will execute some script and set the accepted=yes cookie. So that on the next visit the visitor wont see the popup.
var cookie = document.cookie;
if (cookie = accepted=yes) {
} else {
document.cookie = "accepted=yes";
}
That is the code i have discovered.
You can use getCookie and setCookie Functions
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.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 "";
}
Example
var user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 365);
}
}
Reference: W3Schools https://www.w3schools.com/js/tryit.asp?filename=tryjs_cookie_username
Below are the two functions I currently use inorder to read and create my cookie (cookie should only be valid for 1 day)
function set1DayValidationCookie(){
var d = new Date();
var today = d.getMonth() + '' + d.getDate();
if (readCookie('onedaycookie') != today) {
document.cookie = 'onedaycookie='+today;
console.log('cookie has been created');
} else {
alert('cookie already exist!');
}
}
function readCookie(name) {
var nameEQ = name + "=";
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, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
On refresh of the page, cookie is recognized however
for example, I change my url from www.mywebsite.com/en/ to www.mywebsite.com/ru/
(for language purposes) cookie has been created again.
I would like to ask what I am missing on this part?
You need to set the 'path' part of the cookie to '/'. Then it will Work for all paths.
document.cookie = "username=John Smith; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
Witout the path, the cookie will be set for the current path only.
I would suggest this code
window.cookie = {
set: function(c_name, value, exdays, path = '/') {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : ("; expires=" + exdate.toUTCString())) + "; path=" + path;
document.cookie = c_name + "=" + c_value;
},
get: function(c_name) {
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);
}
}
}
};
And you can check like this:
if("undefined" !== typeof cookie.get('lang')){
//cookie is not set
cookie.set('lang', 'en', 1);
}else{
//cookie is set
}
This doesn't seem to work in the IE Explorer (
Tools - Internet Options - Settings - View files)
If you didn't know already, it is the cache explorer. Now my question is "What is wrong with this?"- It does not write to it. This is the code. Feel free to improve it.
function writeCookie(name,value,days) {
var date, expires;
if (days) {
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 readCookie(name) {
var i, c, ca, nameEQ = name + "=";
ca = document.cookie.split(';');
for(i=0;i < ca.length;i++) {
c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1,c.length);
}
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length,c.length);
}
}
return '';
}
var sId = 's';
writeCookie('sessionId', sId, 3);
var sId = readCookie('sessionId');
document.write(sID);
I was wondering if there is a way to set session variable in Ajax response, which would basically look like this:
// this is response
function(response)
{
<?php $_SESSION['ID'] = response.id ?>
// or something similar to this?
}
And then afterwards when I need to use the session so that I can access it.
// (Some php file)
$var_id = $_SESSION['ID'];
Is this doable?
Here are three functions to set, get, and delete cookies
https://jsfiddle.net/stevenkaspar/gnoj9aop/
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
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 deleteCookie(cname){
document.cookie = cname+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
}
You can use HTML5 sessionStorage Object to save the data locally but for a single session . The data is deleted when the user closes the specific browser tab.
sessionStorage.id = response.id ;
I want to display a popup once in per browser session in a day. So I collect this below script from here. But this script dose bot display anything anytime. Please help me to solved it.
Note: without script popup display well for every page start. But I just want once for my site per browser session.
Cookie script:
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
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("ccname");
if (user != "") {
} else {
$(".socialbox").fadeIn();
$(".smallcontainers").fadeIn()
user = "mysocial";
setCookie("ccname", user, 1);
}
}