5 Replies Latest reply on Jan 5, 2010 6:50 AM by gvasileiou

    How to Set cookie in AIR with HTML/Javascript

    Tickster
      Can someone explain or post example code as to how I can go about setting a cookie in AIR using HTML and Javascript?

      I have an AIR app that requires the user to login on a form that lives in a non-applicationSandbox. I know how to create/modified/delete cookies using JS and a web browser, but the same code returns a cookie with a null value. I'm sure I'm just missing one line/word of code to make this happen.

      The stripped down basics of setting and reading a cookie using Javascript:

      document.cookie("myCookie = yummy");
      var taste = document.cookie;

      What am I doing wrong?
        • 1. Re: How to Set cookie in AIR with HTML/Javascript
          anirudhs Level 2
          Hi,

          Cookies will work when you are accessing the page from a "http"/"https" link (ie, not file:). This is the behaviour inherent in Safari's webkit HTML engine which is what AIR internally uses.

          Try out the code in this page on an AIR html file. It will say, "No Cookies". But if you put a <a> link in your HTML page to that link, navigate to that page and click on "Got Cookies", you will get "Got Cookies!".
          • 2. Re: How to Set cookie in AIR with HTML/Javascript
            Tickster Level 1
            Thanks Anirudhs,
            So if I want to use cookies in AIR html/javascript the end user will need to have http access, whether it be from a public or private server?

            My app has an admin section that allows users with login rights to edit the content. While I can have them login with each edit, I'd rather them login in once per session.
            • 3. Re: How to Set cookie in AIR with HTML/Javascript
              anirudhs Level 2
              > So if I want to use cookies in AIR html/javascript the end user will need to have http access, whether it be from a public or private server?

              Yes.

              Though I am curious to why you are using cookies to store state on the client? This is AIR and you have access to the filesystem, local SQLite DB, encrypted local store, etc.

              • 4. Re: How to Set cookie in AIR with HTML/Javascript
                bitwize3
                quote:

                Originally posted by: anirudhs
                > So if I want to use cookies in AIR html/javascript the end user will need to have http access, whether it be from a public or private server?

                Yes.

                Though I am curious to why you are using cookies to store state on the client? This is AIR and you have access to the filesystem, local SQLite DB, encrypted local store, etc.




                Thanks for your previous answer anirudhs. Finally got a cookie thru Air! :P

                Some scenario's do require login via cookies for our webapps (in my case, chat w/ nicks which also logs users by serverside js + php, thereby allowing the same access method for web-browser login to the same chat rooms). It might be a bit of a stretch for some of us to attempt to code filesystem/SQlite/localStore updater for our web-users via js + php, although I know we could perhaps embed a hidden flash to do the localStore, and pass data back and forth from js, but that's lot of extra lifting, whereas browser cookies are naturally designed for such logins.

                I'm confused by the safari wording thou.. does the webkit security model in Air only work via user activated http (ie. clicking links), or is there some way Air can initiate an http connection (say using loader) to set or get cookies? It seems a pain to need to force the Air user to click and open a dead link just to activate a cookie session, even if it's only to a hidden frame.
                • 5. Re: How to Set cookie in AIR with HTML/Javascript
                  gvasileiou

                  Hi Guys, I am trying to solve the same problem that is described here. The problem is that the link to http://developer.apple.com/internet/safari/faq.html#anchor6 leads to the home page. Is it possible for someone to paste the working example?

                   

                  Thanks