Monday, December 5, 2011

How to create cookie in javascript?

Today I am showing how to create cookie in java script. Here i am giving you an example for how to get the client IP address and save that into the cookie.

Here's the script for this
Put this script within the head tag
 <script type="text/javascript">
        var ipadd;
        function getip(data) {
            ipadd = data.ip;

//This function helping us to create the cookie
 function setvisibility() {
            var a = document.getElementById("hdIp");
            a.value = ipadd;
            var c_name = "clientIPAdd";
            var exdays = 1;
            var exdate = new Date();
            exdate.setDate(exdate.getDate() + exdays);
            var c_value = escape(ipadd) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
            document.cookie = c_name + "=" + c_value;
          //Where c_name(clientIPAdd) is the  name of the and   //c_value is its value and the cookie will expire next day of creation

    <script type="text/javascript" src="">

Here is the page 

<body onload="setvisibility();">
    <form id="form1" runat="server">

        <asp:HiddenField ID="hdIp" runat="server" OnValueChanged="hdIp_ValueChanged" Value="0" />
        <asp:Button ID="myImageButton" runat="server" OnClick="myImageButton_Click" Height="1"
            Width="1" />


No comments:

Post a Comment