0

I'm trying to change a variable depending on what it's current value is:

window.addEventListener("popstate", function(e) {     
    if (direction === leftnav){
        direction = rightnav
    }
    else{
       direction = leftnav
    };

    loadPage(location.pathname);
});

this doesn't seem to work somehow :s can someone help me with this? Thanks in advance :)

EDIT: here is the full js file: http://pastebin.com/7zZseW74

EDIT 2: what seems to happen is that the loadPage function just does not seem to fire...

4
  • What "does not work"? What is the value of direction, leftnav and rightnav? Your example is too vague to be answerable... Commented Apr 3, 2011 at 13:10
  • basicaly, i add css differenly depending on the value of direction. Now i want to intercept a popstate event but invert the direction, so change the variable set earlier. Commented Apr 3, 2011 at 13:19
  • What are direction, leftnav, and rightnav? Commented Apr 3, 2011 at 13:37
  • @Šime direction cathes the class of a parent element. If the class is leftnav it sets it to leftnav, if it is not it sets it to another class Commented Apr 3, 2011 at 13:41

5 Answers 5

1

Try using == instead of ===:

if (direction == leftnav) {
   direction = rightnav;
} else {
   direction = leftnav;
}

Also, do include proper ; inside if and else clauses. You could also provide more information in order to get better help. Information like: what are leftnav and rightnav variables. If they are not variables but literals, you should enclose them within ". Like "rightnav" and "leftnav".

Sign up to request clarification or add additional context in comments.

6 Comments

Actually, last ; is perfectly fine, it should be there. It's for window.addEventListener. Additionally, in Javascript you can have multiple ; without any problem (and missing ones for that matter; those are added automatically).
@Olli: Agree. But question was edited. I didn't have that information when I wrote the answer. :-)
I think he meant the one after if, not the very last one.
@pablo I tried both, it didn't work :(. I've included the full file in the main post now
Oh well, true. Still, little known fact, line breaks are enough, you don't have to use ;, and you can add additional ;'s without problem.
|
0
if (direction == leftnav){

^ requires only 2 '=' signs.

should be correct for the rest.

Comments

0

I believe all you're missing is semicolons.

if (direction == leftnav) {
    direction = rightnav;
} else {
    direction = leftnav;
}

Also, unnecessary ===

Comments

0

try this

if (direction == 'leftnav')
    {direction = 'rightnav';}
else
    {direction = 'leftnav';}

Comments

0

You haven't said a thing about leftnav and rightnav variable, so I suspect that you may want to use strings ('leftnav' and 'rightnav') instead, otherwise both are likely to be undefineds.

EDIT: Now that you posted the code, brief look at it suggests that yes, you wanted quoted strings.

window.addEventListener("popstate", function(e) {
    direction = (direction=="leftnav")?"rightnav":"leftnav";
    loadPage(location.pathname);
});

4 Comments

SnippetSpace, I couldn't find definitions. And I still can't.
direction = $(this).attr('class'); That happens before a popstate in any case
I was talking about leftnav and rightnav, not direction. Now that you say that loadPage() doesn't seem to fire, it's totoally different issue, I guess your event listener doesn't fire at all then. Tried putting debug something in there?
the event does load when i remove the if else statements. And i see what you mean now, leftnav and rightnav are the variable values so i should add quotes around it i guess.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.