I'm working on the option for hiding the panel's bodies and keep them hidden by reloading the page. It's a bootstrap driven code and the operation $("#requests").collapse() produces an animation that I don't like to see cause having 100 of panels collapsing each time by reloading the page can be annoying. So is there some better method to do this?
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 setCookie(cname) {
var d = new Date();
d.setTime(d.getTime() + (365*24*60*60*1000));
var expires = "expires="+d.toUTCString();
if(getCookie(cname) == 1) {
document.cookie = cname + "=" + 0 + "; " + expires;
} else {
document.cookie = cname + "=" + 1 + "; " + expires;
}
}
window.onload = function applyCookies() {
if(getCookie("hidden") == 1) {
$("#requests").collapse();
}
}
if(getCookie("hidden") == 1) {
$("#requests").hide();
}
Add #request{
display:none;
}
Now, you just change your function to this
window.onload = function applyCookies() {
if(getCookie("hidden") == 0) {
$("#requests").css("display","block");
}
}
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
I got this library from google, this cookie consent script. It works well but the problem is that untill you press the button and refresh the page the rest of javascripts won't work, so my navigation on phone isn't working neither the data-scroll animation. This is the code. I don't know where the problem is, if somebody could help me I would be thankful. Have a great day!
// --- Config --- //
var purecookieTitle = 'Cookies.'; // Title
var purecookieDesc =
'Acest website folosește cookie-uri pentru a vă îmbunătăți experiența. Folosind site-ul nostru web, sunteți de acord cu toate cookie-urilor în conformitate cu politica noastră de confidențialitate.'; // Description
var purecookieLink =
'Citesțe mai mult'; // Cookiepolicy link
var purecookieButton = 'Am înțeles'; // Button text
// --- --- //
function pureFadeIn(elem, display) {
var el = document.getElementById(elem);
el.style.opacity = 0;
el.style.display = display || 'block';
(function fade() {
var val = parseFloat(el.style.opacity);
if (!((val += 0.02) > 1)) {
el.style.opacity = val;
requestAnimationFrame(fade);
}
})();
}
function pureFadeOut(elem) {
var el = document.getElementById(elem);
el.style.opacity = 1;
(function fade() {
if ((el.style.opacity -= 0.02) < 0) {
el.style.display = 'none';
} else {
requestAnimationFrame(fade);
}
})();
}
function setCookie(name, value, days) {
var expires = '';
if (days) {
var date = new Date();
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
expires = '; expires=' + date.toUTCString();
}
document.cookie = name + '=' + (value || '') + expires + '; path=/';
}
function getCookie(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) {
document.cookie = name + '=; Max-Age=-99999999;';
}
function cookieConsent() {
if (!getCookie('purecookieDismiss')) {
document.body.innerHTML +=
'<div class="cookieConsentContainer" id="cookieConsentContainer"><div class="cookieTitle"><a>' +
purecookieTitle +
'</a></div><div class="cookieDesc"><p>' +
purecookieDesc +
' ' +
purecookieLink +
'</p></div><div class="cookieButton"><a onClick="purecookieDismiss();">' +
purecookieButton +
'</a></div></div>';
pureFadeIn('cookieConsentContainer');
}
}
function purecookieDismiss() {
setCookie('purecookieDismiss', '1', 14);
pureFadeOut('cookieConsentContainer');
}
window.onload = function () {
cookieConsent();
};
Just in case someone else stumble upon this issue, the problem is in this line:
document.body.innerHTML +=
If you replace it with this, for example, it works perfectly and does not block any other javascript, but obviously you have to add the id footer somewehere in your page
document.getElementById("footer").innerHTML+=
In your HTML, put any div and assign an id to it:
<div class="any" id="footer"></div>
...
In the JS file, in the cookieConsent() function, change
document.body.innerHTML += to document.getElementById('footer').innerHTML +=.
The complete code would look like this:
function cookieConsent() {
if (!getCookie('purecookieDismiss')) {
document.getElementById('footer').innerHTML +=
'<div class="cookieConsentContainer" id="cookieConsentContainer"><div class="cookieTitle"><a>' +
purecookieTitle +
'</a></div><div class="cookieDesc"><p>' +
purecookieDesc +
'' +
purecookieLink +
'</p></div><div class="cookieButton"><a onClick="purecookieDismiss();">' +
purecookieButton +
'</a></div></div>';
pureFadeIn('cookieConsentContainer');
}
}
Sorry for my bad English ;(
When a user click on a button it would take them to a link but before they go to that link, the cookie will need to be set to either English (EN) or French (FR). I got an example here:
http://www.quirksmode.org/js/cookies.html
but for some reason, it's not reading in the cookie and I'm not sure where I'm going wrong.
This is what I have:
<!DOCTYPE html>
<html>
<body>
<p>This is for the Browser Cookies.</p>
<button onclick="EN_Cookie()">English Link</button> <button onclick="FR_Cookie()">French Link</button>
<script>
function EN_Cookie() {
setCookie("SMDCookie","EN",7);
//location.href = 'https://google.com';
}
function FR_Cookie() {
setCookie("SMDCookie","FR",7);
//location.href = 'https://google.com';
}
function setCookie(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=/";
}
</script>
</body>
</html>
Any suggestions??
I reviewed your code and found your set cookie function is correct.
May be your getCookie not working, i am sharing get cookie function:
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 "";
}
Here are functions you can use for creating and retrieving cookies
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 = name + "=" + value + expires + "; 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 "";
}
Copied from How do I create and read a value from cookie with javascript?
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');
}
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>