...

View Full Version : code to create cookie wont work on IE



supercain
10-09-2012, 02:20 PM
i,

i have written this code in order to set a cookie and then make it expire as the user leaves the page:



<script>document.cookie ='chat=sesion; path=index.php';</script>
<script language="javascript">
function clearCookie(){
document.cookie='chat=;expires=Thu, 01-Jan-70 00:00:01 GMT;';
}
window.onunload = clearCookie;
</script>

It works fine on Firefox and Chrome but as usual not on IE (its a piece of shit, you know). Could anybody please recommend me a cross-browser solution?

Thank you.

devnull69
10-09-2012, 03:59 PM
Try onbeforeunload instead of onunload

supercain
10-09-2012, 04:32 PM
Thank you, but actually the part that wont work in the first place is the code that creates the cookie. The part that destroys it i havent had the chance to test it since i need to have the cookie created first.

Philip M
10-09-2012, 04:40 PM
Delete path=index.php


<script type = "text/javascript">
window.onbeforeunload = confirmExit;
function confirmExit() {
return "You have attempted to leave this page. Are you sure you want to exit this page?";
}
</script>

Note that window.onunload and onbeforeunload raise a dialog.

supercain
10-09-2012, 04:54 PM
No sorry, it didnt work. I deleted path=index.php but it still wont create the cookie. The confirmation window is not necessary, i just want to destroy the cookie.

Philip M
10-09-2012, 05:01 PM
function setCookieValue() {
var name="myname";
var value="set";
var expiry = new Date();
expiry.setTime(expiry.getTime() + (100*60000)); // 100 minutes
alert ("Cookie set again");
document.cookie = name + "=" + value + ";expires=" + expiry.toGMTString() + "; path=/";

}

supercain
10-09-2012, 05:06 PM
Thanks but where do i have to put that? in head? i have to put the code within body because the header is the same for all pages and i need to create the cookie in only one of them.

Thank you.

Philip M
10-09-2012, 05:13 PM
Thanks but where do i have to put that? in head? i have to put the code within body because the header is the same for all pages and i need to create the cookie in only one of them.

Thank you.

Place the code right ahead of the </body> tag in the relevant page.

Can I ask the purpose of a cookie which expires as soon as the user leaves the page?

supercain
10-09-2012, 05:15 PM
Hehe, i forgot to mention the footer is the same for all pages too. Sorry. Wot it work if i put it anywhere else inside the body?

Philip M
10-09-2012, 05:19 PM
Hehe, i forgot to mention the footer is the same for all pages too. Sorry. Wot it work if i put it anywhere else inside the body?

Why not try it?

I don't see what the HTML code footer has to do with it.

supercain
10-09-2012, 05:28 PM
Because you said i have to put it right ahead the closing body tag, and the only way to do that is to put it in the footer. But i will try anyway. How should i call the function? by the way, the purpose is to create a chat session that lasts as long as the user is on that window so the session has to be destroyed as he leaves the page. The chat detects whether or not the user is already on the chat window with the session cookie, this way the user cant have multiple pages open at the same time.

supercain
10-09-2012, 06:12 PM
Never mind. I solved it. The original code did the trick, i just modified a little to this


<script>document.cookie ='chat=sesion; path=/';</script>
<script language="javascript">
function clearCookie(){
document.cookie='chat=;expires=Thu, 01-Jan-70 00:00:01 GMT;';
}
window.onunload = clearCookie;
</script>

IE actually creates the cookie, its just it doesnt give it the right value, but that doesnt matter because i just modified the code that reads the cookie so it only checks whether or not the cookie exists, regardless of its value.

Thank you everyone!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum