用客户端的IP设置客户端cookie

时间:2014-05-01 20:02:31

标签: javascript cookies

我需要创建一个没有服务器或服务器端代码的用户登录表单。

为此,我只是将用户cookie设置为用户的IP。我知道这不是最好的,但它应该足以收集有趣的活动的公司内部注册表格,对吗?

我只是希望提交表单能够自动打开填写了数据的电子邮件。但我只希望这种情况发生在唯一的IP上,所以我打算将唯一的IP存储在cookie中10天。

由于某种原因,log(clientip)显示IP,但我无法将cookie设置为IP。为什么?它提供了错误:Uncaught TypeError: undefined is not a function用于设置Cookie($.cookie("client_ip_cookie", clientip, { path: '/', expires : 10,});

Index.html:收集IP:

<script type="application/javascript">
    var clientip = "";

    function getip(json){
        clientip = json.ip; 
    }

</script>

<script type="application/javascript" src="http://jsonip.appspot.com/?callback=getip"> </script>

提交表格:

    <div class="eventForm"> 
        <p>Name: <br> <input type="text" id="name" name="name" value=''></p>
        <p>Email:<br> <input type="text" id="email" name="email" value=''></p>
        <p>Message:<br><textarea id="message" name="message" rows=8 cols=30></textarea></p>
        <button id="submitBtn" value="Submit" name='submit'>Submit</button>
    </div>  

JavaScript的:

        $("#submitBtn").click(function (event) {
            log(clientip); //logs IP
            $.cookie("client_ip_cookie", clientip, { path: '/', expires : 10});

            //if (clientip is not on page, then log these credentials)
            var subject = "Registration for Walk-a-thon",
                name = document.getElementById("name").value,
                email = document.getElementById("email").value,
                message = document.getElementById("message").value;

            var link = "mailto:testemail@aol.com"
                     + "?cc=myemail@aol.com"
                     + "&subject=" + escape(subject)
                     + "&body=" + escape(message)
            ;

            window.location.href = link;    
        });

1 个答案:

答案 0 :(得分:0)

如果您的网页尚未加载jquery.cookie插件,则$.cookie将无法定义。

包含脚本

<script src="/path/to/jquery.cookie.js"></script>
在 jQuery库之后

另见$.cookie is not a function