5

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 != ''
7
  • 8
    On which line is the error thrown? This is like calling 911 and then not giving your address. Line number, please! Jun 29, 2013 at 19:33
  • Funny man... @ŠimeVidas
    – Jayram
    Jun 29, 2013 at 19:43
  • 1
    What IDE are you talking about? The error message doesn't make sense for JavaScript code.
    – Pointy
    Jun 29, 2013 at 19:50
  • If I do some research on the web, I think your IDE might be figuring out the type of an variable via the documentation, and notifies you that you are trying to compare or alter a HTMLCollection with a string, and that the string cannot be converted to a HTMLCollection. (Therefore, the comparisation would make no sense or both variables could not be combined with that operator. Depending on what you have in name (or what the IDE thinks is in it), that might be causing it.)
    – Sumurai8
    Jun 29, 2013 at 20:02
  • name is the string 'csrftoken' Jun 29, 2013 at 20:03

3 Answers 3

4

The problem is that Jetbrains PyCharm/Webstorm has "document.cookie" defined as @type {HTMLCollection}, as you can see when Ctrl-clicking on the 'cookie' variable (that will open the file "DHTML.js" containing the definition). That definition is wrong, all technical docs for 'document.cookie' describe it as a string, see for instance http://www.w3schools.com/jsref/prop_doc_cookie.asp Actually it was also reported as bug WEB-11410 in Jetbrains tools, and seems to be fixed now in the newer builds.

If you want to remove the warning, either add the inline comment as suggested by gztomas, or press Alt-Enter on the warning and go to "inspection 'type mismatch' options" and then "suppress for statement", that will disable the warning for the current statement only.

Changing the comparison to !== as suggested by Gannon does not help. Actually the comparison works correctly with !== also, most probably because this is just an issue with the IDE, 'document.cookie' is a string in all browsers.

(I don't have enough reputation for commenting on previous answers, so adding this as extra answer...)

1
  • this looks like a pretty good answer, referencing the related bug and more to actual documentation of what it should be.
    – dashesy
    Jan 20, 2015 at 22:43
0

Line 3:

if (document.cookie && document.cookie != '') {

The IDE is warning that the empty string '' is a string type, and there is an implicit cast resulting from != of the empty string to the type of document.cookie, which is an HTMLCollection type.

3
  • That did not change it I'm afraid. Jun 30, 2013 at 7:40
  • I would not expect the warning to go away; if you use !== there won't be an implicit type conversion and your IDE error would go away, however your test would never return true becuas !== would check the types and return false. There is a post about converting HTMLCollection type objects into strings. Jun 30, 2013 at 14:19
  • If Jetbrains Pycharm works like Jetbrains IDEa, you could probably change the inspection profile setting? Jul 2, 2013 at 21:43
0

Try modifying line 3 to:

if (document.cookie && document.cookie != /**@type HTMLCollection*/'') {

Your IDE probably understands document.cookie as being of type HTMLCollection. The inline comment works as a cast for the interpreter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.