In this code I am checking if a cookie is set and running a code to pop up a flag selection. If the cookie is not set it will set the cookie once a flaf is checked. It works in chrome but wont work on firefox. I used the code from WC3. Can someone help me find the issue here. Thank you in advance.
<script type="text/javascript">
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 "";
}
function checkCookie() {
var languageSet = getCookie("setLanguage");
if (languageSet != "" || languageSet != null) {
return null;
} else {
jQuery.fancybox("#selectFlagDiv");
setCookie("setLanguage", setLanguage, 365);
}
}
</script>
<div id="selectFlagDiv" onload="checkCookie();" style="display:none;">
<img style="margin-bottom:18px;margin-top:18px;width:300px;height:180px;" src="1.gif" onclick="setLangStorage();" />
<img style="margin-bottom:18px;margin-top:18px;width:300px;height:180px;" src="2.gif" onclick="setLangStorage();" />
</div>
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
Why does on my page http://omt-serwis.pl cookie is set (the cookie popup does not appear again GLOBALLY on the whole page) only when I click cookie checkmark on the Home Page (http://omt-serwis.pl), but not on the subpages for instance http://omt-serwis.pl/kontakt/?
This is my code:
HTML:
<div class="june2018cookies">
<p class="info">Strona korzysta z plików cookies w celu realizacji usług. Możesz określić warunki przechowywania lub dostępu do plików cookies w Twojej przeglądarce.<br>
Dowiedz się więcej.</p>
<div class=""></div>
<i class="far fa-check-circle june2018cookies-close"></i>
</div>
JS:
var PopUpCookie = getCookie("MyPopUpCookie");
if (PopUpCookie == '') {
setTimeout(june2018cookie, 0);
} else {
}
jQuery('.june2018cookies-close').on('click', function () {
setCookie("MyPopUpCookie");
jQuery('.june2018cookies').addClass('offsetright');
});
function setCookie(cname, cvalue) {
var d = new Date();
d.setTime(d.getTime() + (5*24*60*60*1000)); /* 2 days */
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 june2018cookie() {
jQuery('.june2018cookies').animate({
"right": "+=350px"
});
jQuery('.june2018cookies').removeClass('offsetright');
}
This script keeps track of the number of times a surfer has visited page, and show the surfer of this info. Also with buttons to allow him/her to reset info. When the "Revisit Page" button is clicked, it refresh the page. Get the message according to the number of visits and print it. When the "Reset Counter" is clicked, it set the counter to zero and refresh the page. I did coding almost but it's not working. Can anyone tell me what's wrong in my code ?
code:
<!DOCTYPE html>
<html>
<head>
<title>cookie</title>
<style>
</style>
</head>
<body>
<div class="myDiv" id="div">
<p id="txt"> </p> </div>
<FORM>
<CENTER>
<INPUT NAME="update" TYPE="BUTTON" VALUE="Revisit Page" OnClick="history.go(0)" id="revisit">
<INPUT NAME="reset" TYPE="BUTTON" VALUE="Reset Counter" OnClick="ResetCounts()" id="reset">
</CENTER>
</FORM>
<script>
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
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 "";
}
function DisplayInfo() {
var expdate = new Date();
var visit;
expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365));
if(!(visit = GetCookie("visit")))
visit = 0;
visit++;
SetCookie("visit", visit, expdate, "/", null, false);
var message;
if(visit == 1)
message=" Welcome to my page!";
if(visit== 2)
message=" I see you came back !";
if(visit == 3)
message=" Oh, it's you again!";
if(visit == 4)
message=" You must be curious!";
if(visit == 5)
message=" You're practically a regular!";
if(visit == 6)
message=" You need a hobby!";
if(visit == 7)
message=" Nothing better to do?";
if(visit == 8)
message=" Don't you ever sleep?";
if(visit == 9)
message=" Get a life!!!";
if(visit >= 10)
message=" Rent is due on the 1st of the month!";
var txt = document.getElementById("txt").innerHTML = "Your browser has visited this page " + visit + " time(s)." + message;
}
function ResetCounts() {
var expdate = new Date();
expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365));
visit = 0;
SetCookie("visit", visit, expdate , "/", null, false);
history.go(0);
}
window.onload=DisplayInfo
</script>
</body>
</html>
Try these two functions instead - they work on my eCommerce site.
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 "";
}
Also make sure your own browser accepts cookies and isn't purging them while you're running tests.
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);
}
}
I'm running into a problem with setting a value for a cookie. Everytime I run the function I made, it is always coming back with undefined for the cookie value. Here is my code:
login.html -
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;
}
$('#submit').click(function() {
if ($('#username').val() != "" && $('#password').val() != "") {
// set the cookie
// and redirect
setCookie("username", $('#username').val(), 365);
window.location = "profile.html";
} else {
alert("All fields must be filled out");
}
});
and on the profile.html page:
function getCookie(cookie_name) {
var name = cookie_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);
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}
function checkCookie() {
var user = getCookie("username");
if (user != "") {
$('#user_greet').html("<b>Welcome " + user + "</b>");
} else {
alert("You are not allowed to be here");
window.location = "login.html";
}
}
$(document).ready(function() {
checkCookie();
});
Any help would be appreciated.
Thanks!