It's hard to even describe the question. I can't reproduce a snippet, obviously because it requires using cookies, but I will try to reproduce it with a normal array, and show you how it should work, then I'll show you screenshots of my code, and the outcome it produces when used on real cookies.
function cookiename(name) {
//var test5 = document.cookie.split(";");
var test5 = ["user=Jim Jordan", "color=blue", "cat=bella", "username=NikoaTesla"];
var username2 = name;
var output = "";
if(test5[0].indexOf("user") == 0) {
output = test5[0].substring(username2.length, test5[0].length);
} else alert("IT DOES NOT WORK");
alert(output);
}
cookiename("user");
This is pretty much what my code looks like, except that, instead of array, test5
is assigned to document.cookie.split(";")
, and it contains two more cookies.
Now, the way it works is, you create a conditional statement with the array value, in this case, test5[0]
, which contains the value "user=Jim Jordan"
, and say, if the indexof("user")
string is in position 0 inside the test5[0]
string, which contains the value user=Jim Jordan
, then execute the condition, if not, alert that it doesn't work.
Now, as you saw, it works great in the above example. It works as expected with any of the other array values. test5[1]
, test5[2]
etc. will work the same way, of course in the above example they won't match the condition, but if you change the indexof
string, it works.
Now, the issue I have is that, the test5
variable stores the document.cookie.split(";")
array, and only the first array value works, while the others don't, even though the condition should be matching. However, the other values do work but only if the indexof
string is intentionally wrong, and doesn't exist inside the array value, and the condition is of course -1. If the indexof
string actually exists, both 0 and -1 conditions don't match. Very strange.
Here's a screenshot of my code, and subsequent result:
So, as you can see, the first value works as expected.
But then, when I try with another array value, it doesn't work. The third array value is called username=Sam Jones
. This is what happens when I change indexof("user")
with indexof("username")
.
As you can see, the prior alert that I inserted displays that test5[2]
contains the value of username=Sam Jones
, but then when use it as a condition, the indexof("username")
does not match it. It should be 0, but it's not. Even when I try -1, instead of 0, which matches strings that do not exist, it still produces the exact same outcome! Why!?
Now, watch what happens when I add a string in indexof
that does not exist. Instead of the string username
, I will add something random, and use -1 as a condition.
Different indexof string on Third array value
As you see, now the random indexof
string matches the -1, because it doesn't exist. But why when the indexof
string actually does exist, neither 0 nor -1 match the condition?
Why only the first array value work?
Does anyone have any idea what is happening here?