Sanjay Rathod
Sanjay Rathod

Reputation: 1143

How to get cookie on load event of body?

i have one dialog box like this

<div id="savedialog"  class="pop1">      
    <div id="center" style="margin:30px;">
        <h1 style="font-size:20px;color:#000;margin-bottom:0;">Ready to Save Your Quote?</h1>
        <p style="font-size:12px;margin-bottom:0;">
            <span style="font-weight:bold;">Email</span>
            <input style="margin-bottom:15px; margin-left:90px;" type="text" id="email"  name="email" placeholder="" />
            <p style="font-size:12px;margin-bottom:0;">
                <span style="font-weight:bold;">Confirm Email</span>
                <input style="margin-bottom:15px; margin-left:40px;" type="text" id="confirm_email"  name="confirm_email" placeholder="" />
            </p>
            <hr style="border-bottom:1px dotted #ccc;">
        </p>
        <p>
            <input type="hidden" name="action" value="popshow"/>    
            <input class="btn btn-primary btn-large" type="button" id="directory" value="Save quote" > &nbsp;&nbsp;&nbsp;
            <a href="#" id="cnl24">Cancel</a>
        </p>
    </div>
</div> 

Now i had show and hide dialog box by following code and set cookie in that code like this

$(function(){
    $('#saveandcontinue').click(function() {    
        $('#savedialog').show("slow");         
    });

    $('#directory').click(function(){
        var email = document.getElementById('email').value;
        document.cookie="emailID="+email; "path=/";
        $('#savedialog').hide("slow");
        location.href='03_drivers.php';
        alert("your Quotre has been saved");
    });

    $('#cnl24').click(function(){
        $('#savedialog').hide("slow");
    });
});

and i have call a function for check cookie like this

<body onload="checkCookie();">

and defined check cookies by checkcookie function

function checkCookie() {
    alert("yes");
    var user=getCookie("email");

    if (user!="")
        alert("Welcome again ");
    else
        alert("not set");
}

but didnot get any cookie so please can you suggest me where i am wrong.

Upvotes: 0

Views: 2311

Answers (1)

Suman Bogati
Suman Bogati

Reputation: 6349

In case of you are not defined getCookie function, please use this one

function getCookie(cname){
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) 
      {
      var c = ca[i].trim();
      if (c.indexOf(name)==0) return c.substring(name.length,c.length);
      }
    return "";
}

And you are setting the cookie with 'emailId' with document.cookie="emailID="+email; "path=/"; and getting with email =getCookie("email");

Upvotes: 2

Related Questions