0

How do i do this, W3Schools is not helpful here is my code

document.cookie="krul=1";
var x = document.cookie;
if(x == 1){
    document.getElementById("krul").src="https://www.youtube.com/v/An9M93j2XUs?version=3&loop=1&playlist=An9M93j2XUs&autoplay=1";
}

I created a cookie kruuuul=1 then I try assingning it to x and comparing it in a if statement.

QuietDaniel
  • 13
  • 1
  • 6
  • Use one of these functions to get the cookie by name: http://stackoverflow.com/questions/10730362/get-cookie-by-name – MFazio23 Feb 03 '15 at 18:34

2 Answers2

1

document.cookie will return all the cookies that are attached to the page. You have to parse the returned string to find the information that you want. There are many different ways to do this.

This function accepts a cookie name and an optional parameter. The function will return the cookie name and the values assigned or just the value if that's what you want.

function getSpecificCookie(cookieName, valueOnly) {
    //Get original cookie string
    var oCookieArray = document.cookie.split(';'),
        fc,
        cnRE = new RegExp(cookieName + '\=');
    //Loop through cookies
    for (var c = 0; c < oCookieArray.length; c++) {

        //If found save to variable and end loop
        if (cnRE.test(oCookieArray[c])) {
            fc = oCookieArray[c].trim();
            if (valueOnly) {
                fc = fc.replace(cookieName +'=', '');
            }
            break;
        }

    }
    return fc;
}

You would use it like this:

//Set the cookie;
document.cookie = 'krul=1';

//Either call the function to return the entire cookie;
var x = getSpecificCookie('krul');

if (x == 'krul=1') {
    //do something
}


//Or call the function to return just the cookie value;
var x = getSpecificCookie('krul', true);
if (x == 1) {
    //do something
}
  • I don't think you should just tell people 'use this, it just works`. I think better would be explaining the whole issue of cookies in javascript – Zaffy Feb 03 '15 at 19:21
  • I know you're new, so I'm trying to help both of you. If you should? It's up to you but bear in mind that we're here to learn each other not to just copy-paste a piece of code we don't quite understand what it does :) – Zaffy Feb 03 '15 at 21:27
0

The cookie value of 'x' is 'krul=1', not '1' like in your conditional statement.

You may want to parse the cookies prior to the 'if' statement to get the individual cookie value you need. Otherwise, you would need to check if the value of 'x' is equal to 'krul=1'.

Of course the latter method isn't really ideal. The first method would be preferred and easier for you to deal with cookies long term.

There are many ways to accomplish this, but here's the example from W3Schools to get the document cookie value via javascript:

    function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
    }
    return "";
}

So, if you were to add the above snippet to your script then your code should call the 'getCookie' function to get the value.

The new code would then look something like:

document.cookie="krul=1";
var x = getCookie("krul");
if(x == 1){
    document.getElementById("krul").src="https://www.youtube.com/v/An9M93j2XUs?version=3&loop=1&playlist=An9M93j2XUs&autoplay=1";
}
JamesD
  • 147
  • 1
  • 5