Writing Cookies in Javascript.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sreemathy2000
    New Member
    • Oct 2007
    • 40

    Writing Cookies in Javascript.

    I have a fully function webpage based on a Zipcode. I get the Zip code from the user once and write it the cookie and retrieve that everytime.

    I also have another webpage which also asks for the zip code.

    I have to synchronize these two. What i have done for this i accept the zip code also as querystrig parameter.
    So from 1 website when i enter the zip code, in the javascript i have image tag and i calll the otherwebsite and load it internally.

    Code:
    if(document.images)	{	image1 = new Image();    image1.src ="http://mywebsite.com/default.aspx?zip=" + zipcode;	}
    The Problem with this it writes only the cookie and doesn't load the webpage.

    When i open the other wbsite i soem times see the cookie written and soem times i doesn't work.
    So i'm not sure where the problem is.

    Is there a better way of synchronizing the Zipcode values in both the websites.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Are the 'websites' from the same domain?

    Comment

    • sreemathy2000
      New Member
      • Oct 2007
      • 40

      #3
      No they are not from the same domain. there are 2 independent websites. We are just trying to make the user enter the Zip code only once.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        You could load the other website in an iframe or via Ajax (though you'd need a web proxy to avoid cross-domain issues).

        The second website should have code to accept the zip parameter and write the cookie, so when you next visit the website the zipcode is set.

        Comment

        • sreemathy2000
          New Member
          • Oct 2007
          • 40

          #5
          Thanks for the replies...Will try it out..

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            No problem, let us know if you hit any problems.

            Comment

            • sreemathy2000
              New Member
              • Oct 2007
              • 40

              #7
              I implemented it using a iframe . It have a cookie in the main website which checks to see if the second website is loaded only once to avoid hit issues.

              code is

              Code:
              function load() { var zipcode = getCookie('Zip'); if(zipcode) { 	var IsCookie= setCookie('OnDemand');	if ((!IsCookie) || (Iscookie !=zipcode ))	{		document.write('<IFRAME SRC="http://myweebsitecom/zipcode.aspx?zip=' + twzipcode + '" WIDTH="0"HEIGHT="0"FRAMEBORDER="0"></IFRAME>'); 	var expdate = new Date(); expdate.setFullYear(expdate.getFullYear() + 1);	setCookie('OnDemand',zipcode,expdate);	}}}
              But i'm facing a problem with this implementataion in IE.
              The second website that i'm loading in the iframe has to be a trusted site to write the client side cookies which cannot be done.
              How can a solution be arrived for this issue without give full trust to the website manually.

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by sreemathy2000
                The second website that i'm loading in the iframe has to be a trusted site to write the client side cookies which cannot be done.
                How can a solution be arrived for this issue without give full trust to the website manually.
                What do you mean by a trusted site?

                Why not have cookie writing code in the other site too? Once it's set there, it should be set when you next visit that site.

                Comment

                • sreemathy2000
                  New Member
                  • Oct 2007
                  • 40

                  #9
                  yes.there is a cookie written in the other site also. But only if we set the website as a trusted site, the website writes the cookie if loaded in iFrame. Otherwise, the client side cookies are not accepted by the browser.

                  We found a solution for this by making the other website use a P3P policy file for accepting client cookies . It works with IE7 , but it doesn't work with IE6.

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    That must be because of your browser settings. If you allow cookies for sites other than trusted ones, it should work.

                    Comment

                    Working...