I was looking at a Function that Django uses to get the CSRF Token, when you need to validate a user session, using Jquery and JavaScript. Here is the code:
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;
}
var csrftoken = getCookie('csrftoken');
You can also see the code online here on the djangoproject website, here.
Okay, now when I paste this code, I get a light warning from my IDE saying this:
Binary operation argument type String is not assignable to type HTMLCollection
What does it mean by this? The error is on this line:
document.cookie && document.cookie != ''