summaryrefslogtreecommitdiff
path: root/tbc/static/js/hitcount-jquery.js
blob: e7106b333fa28b9e935b02ffcb2b3dc97c18da1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
$(document).ready(function() {
  /**
   * https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax
   *
   * Remember you will need to ensure csrf tokens by adding:
   * @ensure_csrf_cookie to your views that require this javascript
   *
   * Also, you will probably want to include this with your other sitewide
   * javascript files ... this is just an example.
   */

   if ( typeof hitcountJS === 'undefined' ) {
    // since this is loaded on every page only do something
    // if a hit is going to be counted
    return;
   }

  var hitcountPK = hitcountJS['hitcountPK'];
  var hitcountURL = hitcountJS['hitcountURL'];
  var csrftoken = getCookie('csrftoken');

  $.ajaxSetup({
      beforeSend: function(xhr, settings) {
          if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
              xhr.setRequestHeader("X-CSRFToken", csrftoken);
          }
      }
  });

  $.post( hitcountURL, { "hitcountPK" : hitcountPK },
    function(data, status) {

      console.log(data); // just so you can see the response

      if (data.status == 'error') {
        // do something for error?
      }
    }, 'json');
});

function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie != '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = jQuery.trim(cookies[i]);
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) == (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}

function csrfSafeMethod(method) {
    // these HTTP methods do not require CSRF protection
    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}