开发者

How to change a javascript variable?

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 jus开发者_StackOverflowt does not seem to fire...


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".


if (direction == leftnav){

^ requires only 2 '=' signs.

should be correct for the rest.


I believe all you're missing is semicolons.

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

Also, unnecessary ===


try this

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


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);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜