How do I set a cookie using an options bar? Is their a way of making an options bar in chrome alerts? Or can you set cookies using variables from forms? Sorry for asking such a stupid question, i am just not sure.
Asked
Active
Viewed 27 times
-1
-
What an an "options bar"? – Quentin Jan 31 '16 at 19:37
-
It isn't hard to find how to [deal with cookies in JS](http://stackoverflow.com/questions/4825683/how-do-i-create-and-read-a-value-from-cookie) – Quentin Jan 31 '16 at 19:38
1 Answers
0
Not sure on "making an options bar in chrome alerts", however I can answer how to set cookies based on variables from a form, this assumes jQuery and also that you want to get the form input upon submitting your form, it also assumes you want to get your cookie value on page load:
$(document).ready(function () {
var myOption = getCookie("savedoption");
if (!myOption){
//no cookie found
}
else{
//perform your cookie logic
}
})
$('#myForm').submit(function(e) {
e.preventDefault();
var myOption = $('#myFormInput').val();
setCookie("savedoption", $('#myFormInput').val(), 1);
this.submit();
});
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);
}

edencorbin
- 2,569
- 5
- 29
- 44