Skip to content

Instantly share code, notes, and snippets.

@ChaseH88
Created February 28, 2019 12:03
Show Gist options
  • Save ChaseH88/6bcf5d5a5d1aec263f4ad504bffe1e4a to your computer and use it in GitHub Desktop.
Save ChaseH88/6bcf5d5a5d1aec263f4ad504bffe1e4a to your computer and use it in GitHub Desktop.
Dashboard Animations - Using a Cookie
<input id="enableAnimations" type="checkbox" value="on"/>
<label for="enableAnimations">Dashboard Animations On</label>
<button onclick="getCookie()">TEST</button>
$(document).ready(function() {
// Dashboard Animations
getCookie(); // Get page defaults
var $animations = $("#enableAnimations");
$($animations).on("change", function() {
var $value = $(this).val().toString();
if($value === "on"){
$(this).val("off");
setCookie("animationcookie", "animationcookie0", 365);
$("#enableAnimations").next().text("Animations Off");
} else {
$(this).val("on");
setCookie("animationcookie", "animationcookie1", 365);
$("#enableAnimations").next().text("Animations On");
}
getCookie();
});
}); //Doc Ready
function getCookie() {
var cookiesArray = document.cookie.split("; ");
var filterArray = [];
for (var i = 0; i < cookiesArray.length; i++) {
var nameValueArray = cookiesArray[i].split("=");
// Adds defaults depending on Cookies
if (nameValueArray[0] === "animationcookie" && nameValueArray[1] === "animationcookie1") {
$("body").addClass("animations");
$("#enableAnimations").prop("checked", true);
$("#enableAnimations").next().text("Animations On");
} else {
$("#enableAnimations").prop("checked", false);
$("#enableAnimations").next().text("Animations Off");
$("body").removeClass("animations");
}
}
}
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=/";
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
.animations {background-color: blue;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment