0

I am developing an open source script for the cookie law that we have in Europe (actually, only in some countries in Europe).

I'm trying to add a functionality that detects wether the user that is currently browsing a website is from Europe or not, and to show the plugin only for those in Europe.

Freegeoip.net has a REST service that seems to suit well to my needs, but I have found that sometimes it is very slow (this could depend on the connection, but I'd prefere not to risk anyway) and there is a 10.000 queries per hour limit. Since my plugin is distributed through jsdelivr, a few big websites could possibly reach that limit.

So, here comes my question: how can I detect if that AJAX request is taking too long (let's say more than 3 seconds), and show the bar anyway?

It's a pure javascript plugin, here is the code:

var cookieLawStates = [
  'BE',
  'BG',
  ...
];

var checkEurope = new XMLHttpRequest();
checkEurope.open('GET', 'https://freegeoip.net/json/', true);
checkEurope.onreadystatechange = function() {
  if (checkEurope.readyState === 4 && checkEurope.status === 200) {
    var country = JSON.parse(checkEurope.responseText).country_code;

    if (cookieLawStates.indexOf(country) > -1) {
      // some other logic before showing the bar
    } else {
      console.log('Not an EU user. cookieBAR is shut down.');
    }
  }
}
checkEurope.send();

EDIT: thanks mplungjan for pointing me to another answer, even if it was a bit off topic here since I'm using plain javascript and not jquery... but it led me to this answer, which is exactly what I need.

Community
  • 1
  • 1
ToX 82
  • 1,064
  • 12
  • 35
  • Set a timeout and a "error fallback" function – DonCallisto Jun 29 '15 at 13:29
  • Suggestion: Always just show the bar anyway. According to the link, "_Cookie bar makes it simple and clear to visitors that cookies are in use and tells them how to adjust browser settings if they are concerned._" Is there any reason not to do this for all visitors? – BSMP Jun 29 '15 at 13:32
  • For what it's worth, I've already seen this (or something very similar) on a lot of sites anyway so you wouldn't be the only one making this notice visible to all visitors. – BSMP Jun 29 '15 at 13:33
  • 1
    I know that several scripts show the bar to everyone, disregarding where they are from. But I think that this is a waste of space for (the rest of the world outside of EU), so I'd prefer to give this as an option to the site owner. – ToX 82 Jun 29 '15 at 14:56
  • It is also a waste of space in Holland and should never ever have been made into law :( – mplungjan Jun 29 '15 at 15:07

0 Answers0