How to detect if the user has cookies blocked

Is there any way to detect if cookies are blocked by a browser?

I thought I could possibly set a cookie then read it to see if it got set. Unfortunately that doesn’t work as Session.Cookies.Value returns whatever I set until the page is refreshed even thogh cookies in fact blocked by the browser.

Interestingly after futsing around with this I was able to get Session.Cookies.Value to return an empty string without refreshing by testing after sending a request to my database. I could only get that to work if I sent the request from a button then tested again with a test button. Perhaps using a timer would make this approach work.

Thanks

After a bit more research and testing, I think I understand this a bit better. Setting a cookie and then testing if it was set would not work unless the http header was updated. Refreshing the browser does just that.

Session.Cookies.Value doesn’t check with the browser to see what the value is, but instead just returns the cookies from the header less any cookies removed with session.Cookies.Removed and including any cookies set with session.Cookies.Set since the header was received.

Am I thinking in the right direction now? If that is the case then anything that refreshes the http header after setting a cookie would allow my idea to work, I think. What could be used to refresh the header without doing a refresh? Probably nothing.

Ok I found the answer. The Javascript funciton navigator.cookieEnabled works perfectly.

For anyone who is interested, here is how I implemented a test for cookies being enabled in my project.

I have a checkbox, cbOptioin, on a login web dialog that says “Remember me”. If they check it, the value changed event runs…

[code]If(cbOption.Value=true) then
ExecuteJavaScript “location.hash = navigator.cookieEnabled;”
TimerCookies.Mode = timer.ModeSingle

end if[/code]

The timer is necessary because the hash tag is not set until after the changed event code above completes. The timer in the dialog has the following action event…

[code]me.Mode = timer.ModeOff

if session.HashTag = “false” then
MsgBox “Cookies are disabled in your browser. To remember you on this computer, you must first enable cookies.”
cbOption.Value = false

end if[/code]

I am sure there are ways to do this without a timer, but I find this pretty simple and it works.

Hi John,

I can’t understand the logic behind this if session.HashTag = "false" then
Any explanation?

In the cbOption onvalue changed event session.Hashtag gets set to true or false from the js function navigator.cookiesEnabled.

Then in the timer I am checking what session.Hashtag has set. If the js script returned false the cookies have been disabled in the browser, and I let the user know that they cannot be remembered on that computer with that browser.

does “session.HashTag” as co-relation with "location.hash " ?

@ronaldo florendo does “session.HashTag” as co-relation with "location.hash " ?

Maybe someone else can answer your question better than me, but yes in essence that is correct. I believe that location.hash is javascript’s reference to session.Hashtag in the web app.

oh wow. This is very useful.

Do you personally discover this or you have documentation for the list of xojo object co-relating with javascript.

I don’t think session.Hashtag is a xojo object.

As I am beginning to understand hashtags the js command location.hash sets a hashtag in the page URL…

      http://mywebsite.com/#true

#true is the hashtag. So ExecuteJavaScript “location.hash = navigator.cookieEnabled;” is simply adding a hashtag, true or false, to the current url. You can see the Hashtag in the address bar after setting it with JavaScript. The Xojo function session.Hashtag just reads it.

I just found out how to remove the hashtag that I just created for my test…

 ExecuteJavaScript("window.history.back(1);")

Using the hashtag is useful for a quick job, but for bigger jobs involving the browser DOM I would highly recommend getting into the WebSDK. Examples and documentation can be found at xojor201Xrx/Extras/WebSDK/