views:

72

answers:

3

I would like to assign a cookie to a variable...

e.g. var aVariable = $.cookie("moo");

I am using the jQuery cookie functions - do I need to set the cookie AND set the variable at the same time or something?

Help pretty please!

^.^

My Code:

function goBackToPosition() {
    var xPos = $.cookie("yourPositionX");
    var yPos = $.cookie("yourPositionY");

        console.log(xPos);
        console.log(yPos);

    if (xPos) {
        alert("xpos!");
        $(this).animate({
            top:    yPos,
            left:   xPos
    }, 400, function(){
            $.cookie("yourPositionX", null);
            $.cookie("yourPositionY", null);
    });

    }
}

I get the following error: a.ownerDocument is undefined

A: 

You might mean.

 $(this).css({
            top:    yPos,
            left:   xPos
        }).fadeIn(400, function() {
            $.cookie("yourPositionX", null);
            $.cookie("yourPositionY", null);
        });

The syntax you had didn't make sense. You were passing a function, but the body of the function looked like an object literal. As balupton said, you may need to combine with fadeIn.

{
   top:    yPos,
   left:   xPos
}

is the same as:

{
   "top":    yPos,
   "left":   xPos
}

It creates an object with top and left properties with the values yPos and xPos, respectively.

Matthew Flaschen
+3  A: 

Your syntax is wrong here:

$(this).css(function() {
    top:    yPos,
    left:   xPos
}, 400, function() {
    $.cookie("yourPositionX", null);
    $.cookie("yourPositionY", null);
});

Perhaps you meant:

$(this).css({
    top:    yPos,
    left:   xPos
}).fadeIn(400, function(){
    $.cookie("yourPositionX", null);
    $.cookie("yourPositionY", null);
});

Or:

$(this).animate({
    top:    yPos,
    left:   xPos
}, 400, function(){
    $.cookie("yourPositionX", null);
    $.cookie("yourPositionY", null);
});

In response to your update / new issue:

$(this) will not work in the scope of goBackToPosition as this references that function, rather than an element. You will have to change $(this) to $('selector') to get it to work.

Or use goBackToPosition.call($('selector')) if you would like to keep using this. The call function alters the value of this when the called function executes, a similar function is apply.

balupton
Ok, so that got rid of that error. The problem I have is that I tried the animate() function before and I get this error (even with your code): `a.ownerDocument is undefined`
Neurofluxation
Question updated :)
Neurofluxation
Answer updated :)
balupton
perfect, cheers!
Neurofluxation
+1  A: 

this is meaningless in that context. Either replace it with a selector, or give it some meaning:

jQuery.fn.goBackToPosition = function() {
    var xPos = $.cookie("yourPositionX");
    var yPos = $.cookie("yourPositionY");

        console.log(xPos);
        console.log(yPos);

    if (xPos) {
        alert("xpos!");
        this.animate({
            top:    yPos,
            left:   xPos
        }, 400, function(){
                $.cookie("yourPositionX", null);
                $.cookie("yourPositionY", null);
        });

    }
}

So then:

$(selector).goBackToPosition()
Eric
Perfect, thank you Eric!
Neurofluxation