Skip to content
Advertisement

JavaScript get cookies function

I’ve been learning on how to get Cookies from a web page. Below is the code extracted from w3schools.com. I am not sure why they used this line of code in the getCookies(cname) function as shown below. Why should there be a blank space at the start of every string in the array “ca”? And why should we take c.substring(1)?

JavaScript

This is the full code:

JavaScript

Advertisement

Answer

while loop use just remove first empty space;

JavaScript

enter image description here

cookie first char is ' ' value then removing while loop by c.substring(1)
example
    c = '  alertDialog=yes' that means c.chartAt(0) = ' ' ok
while (' ' == ' ') condition true
then
    c set c = 'alertDialog=yes'
    then 2nd loop false
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement