开发者

onSubmit function sends cookie even without submit

I simply try to set cookie when form is submitted but it seems that the function sets cookie on every refresh of page

function Sub(){
var exdays="3000";
var value="asdf";
var exdate=new Date();
var c_name="asdf";
exdate.开发者_运维技巧setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;

alert("asdf"); //just for debugging
return true; }
</SCRIPT>

<form id="myform" action="http://localhost" onsubmit="return Sub();">


The cookie will be sent by the browser on each request for the same URL until it is deleted, it expires, or for session cookies, until a new session is created.

You're setting an expiration, so it's not a session cookie. So your browser will send the cookie on each page load to that URL, regardless of whether you clicked something or not.

Try removing the expiration date, clearing your cookie cache, and restarting your browser.

The cookie will not be present until you submit once. Then the cookie will be present until you close your session (restart the browser).


Do you mean the cookie is changed on every reload or that the cookie exists?

I am assuming here that you are using the cookie to send a bit of information to the server along with the form.

Cookies are persistent, and will stick around until they are changed. It might make more sense to have a hidden field in the form, and change the value of that, instead of setting a cookie.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜