I use jfontsize' to increase/decrease the size of my font on my website.
Does anyone know of a way to remember the settings between the pages and when a user returns to the site?
I've been trying to create a cookie but I'm not having much luck!
Here is what I currently have:
<script type="text/javascript" language="javascript">
function createCookie("textsizestyle", textsize, 365) {
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;
}
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;
}
</script>
would anyone mind giving me some pointers?
Thanks!
Related
I want my page to show certain texts in a hindi language when the language chosen is Hindi.
My code:
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 "";
}
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 + ";path=/";
}
if ((currentUiCluture != null) && (currentUiCluture != undefined) && (currentUiCluture != "")) {
if (currentUiCluture == 'hi-IN') {
$('#AmtFYyear').text(yr + "वित्त वर्ष के लिए जिलावार अर्जित राशि " + "(करोड़ रुपये में)");
}
if (currentUiCluture == 'en-US') {
$('#AmtFYyear').text("District wise Accrued Amount for FY " + yr + " (Rs in Cr.)");
}
}
else
{
$('#AmtFYyear').text("District wise Accrued Amount for FY " + yr + " (Rs in Cr.)");
}
But वित्त वर्ष के लिए जिलावार अर्जित राशि gets converted to डेटा उपलबà¥à¤§ नहीं
Initially i thought its due to empty spaces but that also couldn't resolve the issue.
You are close Just use decodeURI(row) like the following :
source: ["first","second",decodeURI("वित्त वर्ष के लिए")]
I am trying to create a Cookie based redirection that takes someone to a new page once 4 days are passed.
I am expecting my site visitors to visit the promo offer page at least 7 times in the first 10 days so I need to make sure that the Cookie is generated for at least 30 days and the timer starts dynamically on their first visit.
Right now I only have this code and looking to get some help!
<script>
$(document).ready(function() {
if (getCookie('cookiesSet') == "") {
setCookie('cookiesSet', 'yes', 365);
setCookie('passed4days', 'no', 4);
} else if (getCookie('passed4days') == '')
window.location = 'www.facebook.com';
});
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 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 "";
}
</script>
Snippet style as expected:
$(document).ready(function () {
var firstVisitTime = getCookie('firstVisitTime')
if (firstVisitTime != '') {
var soManyDays = ((new Date()).getTime() - firstVisitTime) / (24 * 60 * 60 * 1000)
if (soManyDays > 4) window.location = 'www.stackoverflow.com'
else if (soManyDays > 24) console.log('I should remember you')
} else setCookie('firstVisitTime', (new Date()).getTime(), 366)
})
I have to create, update, delete, get the cookies in my project for different products. So is there any jquery plugin for this. Now i am using this to mange cookies in my project.
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);
}
I need a plugin for this which can also update the previous cookie with new product and their value.
There are several plugins for jQuery cookies. You may try https://github.com/carhartl/jquery-cookie or https://plugins.jquery.com/cookie/ or just Google it. and find many other plugins.
I am trying to append some data on run time..
now I have to store this appended data into client side Cookies.
Any suggestion much appreciated. Thanks in advance.
here is the code and fiddle URL:
$('#add').on('click', function(){
var inputHTML = $('#inputName').val();
$('<li>'+inputHTML+'</li>').appendTo('#inputBox > ul');
});
http://jsfiddle.net/ft26u/12/
You can use localStorage, like this:
$('#add').on('click', function(){
var inputHTML = $('#inputName').val();
$('<li>'+inputHTML+'</li>').appendTo('#inputBox > ul');
var myData = localStorage.cookieData ? JSON.parse(localStorage.cookieData) : new Array();
myData.push(inputHTML);
localStorage.cookieData = JSON.stringify(myData);
console.log(myData);
})
Use these functions:
function createCookie(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 = escape(name) + "=" + escape(value) + expires + "; path=/";
}
function readCookie(name) {
var nameEQ = escape(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 unescape(c.substring(nameEQ.length, c.length));
}
return null;
}
function eraseCookie(name) {
createCookie(name, "", -1);
}
Edit Updated sample to load cookie on load
http://jsfiddle.net/aZXMr/3/
I having problem figuring howto preserve the present state ...Let say I have selected radio button and checkbox in present form and navigate way to a different page but if I want to go back to old page how should I able to see the selected radio and checkboxes in my previous page..
Well you can use cookies to do the same. here is a small code snippet that acts over cookies:
var myCookieHandler = (function () {
return {
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();
}
document.cookie = name + "=" + value + expires + "; path=/";
},
readCookie: function (name) {
var nameEq = name + "=";
var ca = document.cookie.split(';');
var i;
for (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;
},
deleteCookie: function (name) {
this.createCookie(name, null, -1);
}
};
}());
usage:
myCookieHandler .writeCookie("Login","true",2);
var cookieValue=myCookieHandler.readCookie("Login");
myCookieHandler.deleteCookie("Login");
Thus when you come back to this page you read your cookie and do the necessary with the same.
Hope this helps..