Trying to set a cookie onto self.parent.window.opener

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Powell

    Trying to set a cookie onto self.parent.window.opener

    I thought this would work but it seems to not work neither in Netscape
    nor in IE:

    Code:
    <script type="text/javascript">
    <!--
    
    // OBTAINED FROM http://www.javascripter.net/faq/settinga.htm
    
    // NOTE THAT IF YOU SET days TO -1 THE COOKIE WILL BE SET TO YESTERDAY
    AND THUS EXPIRE
    function setCookie(name, value, days) {
    var today = new Date();
    var expire = new Date();
    if (days == null || isNaN(days) || days == 0) days = 1;
    if (days >= 1 || days < 0) expire.setTime(today.getTime() + 3600000 *
    24 * days);
    document.cookie = name + '=' + escape(value) + ';expires=' +
    expire.toGMTString();
    }
    
    
    setCookie('val_chat', '**DELETE**', -1);
    self.parent.window.opener.document.writeln(document.cookie); //
    DELETE PARENT WINDOW'S COOKIE
    self.parent.window.opener.location.href = 'chat.jsp';
    self.parent.close();
    //-->
    </script>
    <noscript><!-- NO EQUIVALENT --></noscript>
    However, instead of the expired cookie being set into the frame's
    parent's window.opener document, nothing is set, however, redirection
    takes place but incorrectly since the cookie's instance still exists
    when the window.opener is refreshed.

    So how have you guys done this correctly where I have gone wrong?

    Thanx
    Phil

  • marss

    #2
    Re: Trying to set a cookie onto self.parent.win dow.opener

    > self.parent.win dow.opener.docu ment.writeln(do cument.cookie); //[color=blue]
    > DELETE PARENT WINDOW'S COOKIE[/color]

    This code write something to parent window document, but not operate
    with its cookie.
    Try to add extra parameter to the function "setCookie" :
    function setCookie(name, value, days, docCookie)
    {
    .....

    Change:[color=blue][color=green]
    >>document.cook ie = name + '=' + escape(value) + ';expires='+
    >>expire.toGMTS tring();[/color][/color]
    To:
    docCookie.cooki e = name + '=' + escape(value) + ';expires='
    +expire.toGMTSt ring();
    ......

    And then:
    setCookie('val_ chat', '**DELETE**', -1,
    self.parent.win dow.opener.docu ment);


    Maybe it works, but I don't test.

    Comment

    • Phil Powell

      #3
      Re: Trying to set a cookie onto self.parent.win dow.opener

      I'm sorry I do not understand this. :(

      Phil

      marss wrote:[color=blue][color=green]
      > > self.parent.win dow.opener.docu ment.writeln(do cument.cookie); //
      > > DELETE PARENT WINDOW'S COOKIE[/color]
      >
      > This code write something to parent window document, but not operate
      > with its cookie.
      > Try to add extra parameter to the function "setCookie" :
      > function setCookie(name, value, days, docCookie)
      > {
      > ....
      >
      > Change:[color=green][color=darkred]
      > >>document.cook ie = name + '=' + escape(value) + ';expires='+
      > >>expire.toGMTS tring();[/color][/color]
      > To:
      > docCookie.cooki e = name + '=' + escape(value) + ';expires='
      > +expire.toGMTSt ring();
      > .....
      >
      > And then:
      > setCookie('val_ chat', '**DELETE**', -1,
      > self.parent.win dow.opener.docu ment);
      >
      >
      > Maybe it works, but I don't test.[/color]

      Comment

      • marss

        #4
        Re: Trying to set a cookie onto self.parent.win dow.opener


        Phil Powell wrote:[color=blue]
        > I thought this would work but it seems to not work neither in Netscape
        > nor in IE:
        >
        >
        Code:
        >  <script type="text/javascript">
        >  <!--
        >
        > // OBTAINED FROM http://www.javascripter.net/faq/settinga.htm
        >
        > // NOTE THAT IF YOU SET days TO -1 THE COOKIE WILL BE SET TO YESTERDAY
        > AND THUS EXPIRE
        > function setCookie(name, value, days) {[/color]
        
        Add extra parameter docCookie:
        function setCookie(name, value, days, docCookie) {
        
        [color=blue]
        >  var today = new Date();
        >  var expire = new Date();
        >  if (days == null || isNaN(days) || days == 0) days = 1;
        >  if (days >= 1 || days < 0) expire.setTime(today.getTime() + 3600000 *
        > 24 * days);
        >  document.cookie = name + '=' + escape(value) + ';expires=' +
        > expire.toGMTString();[/color]
        
        Set desired value to docCookie:
        docCookie = name + '=' + escape(value) + ';expires=' +
        expire.toGMTString();
        
        [color=blue]
        > }
        >
        >
        >     setCookie('val_chat', '**DELETE**', -1);[/color]
        
        Sorry, I was wrong here, last parameter must be
        self.parent.window.opener.document.cookie
        (not self.parent.window.opener.document)
        
        setCookie('val_chat', '**DELETE**', -1,
        self.parent.window.opener.document.cookie);
        [color=blue]
        >     self.parent.window.opener.document.writeln(document.cookie); //[/color]
        
        Function writeln() writes content of document.cookie to
        self.parent.window.opener.document but not set its cookie. To set
        cookie of self.parent.window.opener.document you should do it directly:
        self.parent.window.opener.document.cookie="...". I guess this line of
        code is wrong.
        [color=blue]
        > DELETE PARENT WINDOW'S COOKIE
        >     self.parent.window.opener.location.href = 'chat.jsp';
        >     self.parent.close();[/color]
        
        I don't understand what are you going to achieve with this code and
        can't comment it.
        Sorry for my English
        [color=blue]
        >   //-->
        >  </script>
        >  <noscript><!-- NO EQUIVALENT --></noscript>
        >
        >
        > However, instead of the expired cookie being set into the frame's
        > parent's window.opener document, nothing is set, however, redirection
        > takes place but incorrectly since the cookie's instance still exists
        > when the window.opener is refreshed.
        >
        > So how have you guys done this correctly where I have gone wrong?
        >
        > Thanx
        > Phil[/color]

        Comment

        • Dr John Stockton

          #5
          Re: Trying to set a cookie onto self.parent.win dow.opener

          JRS: In article <1139215821.460 751.310010@z14g 2000cwz.googleg roups.com>
          , dated Mon, 6 Feb 2006 00:50:21 remote, seen in
          news:comp.lang. javascript, Phil Powell <phillip.s.powe ll@gmail.com>
          posted :[color=blue]
          >I thought this would work but it seems to not work neither in Netscape
          >nor in IE:
          >
          >[CODE]
          > <script type="text/javascript">
          > <!--
          >
          >// OBTAINED FROM http://www.javascripte r.net/faq/settinga.htm[/color]

          Clearly a site to be avoided.

          [color=blue]
          >// NOTE THAT IF YOU SET days TO -1 THE COOKIE WILL BE SET TO YESTERDAY
          >AND THUS EXPIRE
          >function setCookie(name, value, days) {
          > var today = new Date();
          > var expire = new Date();
          > if (days == null || isNaN(days) || days == 0) days = 1;
          > if (days >= 1 || days < 0) expire.setTime( today.getTime() + 3600000 *
          >24 * days);
          > document.cookie = name + '=' + escape(value) + ';expires=' +
          >expire.toGMTSt ring();
          >}[/color]

          There's no need to create two Date Objects just to get an Object set to
          24 hours ahead of now.

          To get a Date Object whose value does not matter, use new Date(0)
          which is quicker. To get a new Date Object E with the same value as a
          Date Object D, use E = new Date(+D) .

          3600000 * 24 is better written as 864e5.

          Code posted should be executable as posted. Do not allow your posting
          agent to wrap lines.

          You can advance 24 hours, but the description is in terms of days.
          Civil days do not always have 24 hours; if you describe the expiry in
          days, your users may occasionally get surprised.

          Use var T = new Date() ; T.setDate(T.get Date()+1)

          Your code seems to assume that the value of days is integer, without
          ensuring that.
          [color=blue]
          >So how have you guys done this correctly where I have gone wrong?[/color]

          Your code no doubt does exactly what it should do. You have failed to
          say what you want it to do.

          It seems that days is read from a user control. If you check the
          control with a RegExp you can assure a good value; consider and test

          S = control.value
          OK = /^[+-]?\d+$/.test(S) // repeat if false?
          days = OK ? +S : 1

          I suppose that you have checked the incipient cookie string S with, say,
          alert(S); probably the error whose consequences you refer to lies
          elsewhere.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          Working...