浏览器不会从onclick事件中设置Cookie [英] Browser won't set cookie from an onclick event

查看:160
本文介绍了浏览器不会从onclick事件中设置Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个bug拉我的头发,虽然我确定它是明显的。



以下JavaScript成功设置了我的Cookie:

 < script type =text / javascript> 
$(document).ready(function(){
$ .post('../ setCookie.php',{'region':'test'});
});
< / script>

但是一旦我将相同的代码绑定到 onclick 事件,像这样...

 < script type =text / javascript 
$(document).ready(function(){
$(#us a)。click(function(){
$ .post('../ setCookie.php' ,{'region':'en-us'});
});
$(#uk a)。click(function(){
$ .post ./setCookie.php',{'region':'en-gb'});
});
});
< / script>

< ul>
< li id =uk>< a href =http://www.example.com/uk>
< span>输入英国网站< / span>< / a>< / li>
< li id =us>< a href =http://www.example.com/us>
< span>输入美国网站< / span>< / a>< / li>
< / ul>

..它不再设置cookie!即使相同的代码在中以完全相同的方式调用执行(我一步一步地,看看一切,因为它应该是)。



重复:javascript正在触发。我正在通过setCookie.php,一切都一样...除了在它的结尾处没有cookie。



发生了什么事?






对于任何感兴趣的人来说,这里是我解决的问题:

 < script type =text / javascript> 
$(document).ready(function(){
$(#us a)。click(function(e){
e.preventDefault();
$ .post('../ setCookie.php',{'region':'en-us'},
function(){
window.location =http://www.example.com / us;
}
);
});

$(#uk a)。 e.preventDefault();
$ .post('../ setCookie.php',{'region':'en-gb'},
function =http://www.example.com/uk;
}
);
});
});
< / script>


解决方案

我没有看到任何停止正常链接点击通过?如果你没有阻止正常的 a href 通过,没有任何时间做 $。post

 <$ c $> 

c> $(document).ready(function(){
$('a')。click(function(e){
e.preventDefault();
$ .post ../setCookie.php',{'region':'test'});
});
});

它将停止链接的页面更改,但至少应该通过cookie。现在,如果您想要加载网页,请在请求中添加onComplete方法,这样,一旦Cookie数据成功发送,您就可以继续更改网页。


I'm pulling my hair out with this bug, although I'm sure it's something obvious. Apologies if it is.

The following javascript successfully sets my cookie:

<script type="text/javascript">
$(document).ready(function(){
    $.post('../setCookie.php',{'region':'test'});
});
</script>

But as soon as I tie the same code to an onclick event, like so...

<script type="text/javascript">
$(document).ready(function(){
    $("#us a").click(function(){
        $.post('../setCookie.php',{'region':'en-us'});
    });
    $("#uk a").click(function(){
        $.post('../setCookie.php',{'region':'en-gb'});
    });
});
</script>

<ul>
    <li id="uk"><a href="http://www.example.com/uk">
        <span>Enter UK site</span></a></li>
    <li id="us"><a href="http://www.example.com/us">
        <span>Enter US site</span></a></li>
</ul>

..it no longer sets a cookie! Even though the same code is called an executed in exactly the same way (I step through it fine, and see everything as it's supposed to be).

Repeat: The javascript is firing fine. I am stepping through setCookie.php and everything is the same... except no cookie at the end of it.

What's going on? I'm guessing it's browser security or something?


For anyone who's interested, here's how I solved it:

<script type="text/javascript">
$(document).ready(function(){
$("#us a").click(function(e){
    e.preventDefault();
    $.post('../setCookie.php',{'region':'en-us'},
        function() {
            window.location = "http://www.example.com/us";
        }
    );
});

$("#uk a").click(function(e){
    e.preventDefault();
    $.post('../setCookie.php',{'region':'en-gb'},
        function() {
            window.location = "http://www.example.com/uk";
        }
    );
});
});
</script>

解决方案

I don't see anything stopping the normal link click going through? If you have nothing preventing the normal a href to go through, there won't be any time to do the $.post request before the page changed already.

Try the following:

$(document).ready(function(){
    $('a').click(function(e){
        e.preventDefault();
        $.post('../setCookie.php',{'region':'test'});
    });
});

It will stop the page change for links, but at least should pass the cookie. Now, if you want the page to be loaded as well, then add a onComplete method to the request, so that once the cookie data has been succesfully sent, you can then continue the page change.

这篇关于浏览器不会从onclick事件中设置Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆