-1

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.

b sovs
  • 25
  • 2

1 Answers1

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