Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry code to create cookie wont work on IE

    i,

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


    Code:
    <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.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Try onbeforeunload instead of onunload

  • #3
    New Coder
    Join Date
    Oct 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,081
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Delete path=index.php

    Code:
    <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.
    Last edited by Philip M; 10-09-2012 at 03:51 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    Oct 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,081
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Code:
    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=/";
    
    }
    Last edited by Philip M; 10-09-2012 at 04:09 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    New Coder
    Join Date
    Oct 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,081
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by supercain View Post
    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?
    Last edited by Philip M; 10-09-2012 at 04:16 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #9
    New Coder
    Join Date
    Oct 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,081
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by supercain View Post
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #11
    New Coder
    Join Date
    Oct 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by supercain; 10-09-2012 at 04:38 PM.

  • #12
    New Coder
    Join Date
    Oct 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Never mind. I solved it. The original code did the trick, i just modified a little to this

    Code:
    <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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •