简体   繁体   中英

Why does my javascript else statement stop working

The first if-else statement works as expected, when the address bar variables are there it does the if part and when not there does the else...however, right after the VAR lines that split everything up every else statement i put in does not work

var hasVars = 0;
var loc = window.location.href;
if(loc.indexOf('?')>-1) { hasVars = 1; }

if(hasVars==1) { alert("ttt"); }
else { alert("eee"); }

var query = loc.slice(loc.indexOf('?') + 1);
var vars = query.split('&');
var pair = vars[0].split('=');
var data1 = pair[1].split('.');


if(hasVars==1) { alert("ttt"); }
else { alert("eee"); }

I commented every line out and put them back in and the problem seems to be with the data1 variable line, commented out it works fine but with it the else statement doesn't run

The var query (...) part of your code will only work when there are query parameters. So it is having problems when there are none. One solution would be to place an if there as well.

var hasVars = 0;
var loc = window.location.href;
if(loc.indexOf('?')>-1) { hasVars = 1; }

if(hasVars==1) { alert("ttt"); }
else { alert("eee"); }

if (hasVars==1) {                             // added this
    var query = loc.slice(loc.indexOf('?') + 1);
    var vars = query.split('&');
    var pair = vars[0].split('=');
    var data1 = null;
    if (pair[1] != undefined) {               // added this for: "page?var" cases
        data1 = pair[1].split('.');
    }                                         // added
}                                             // added

if(hasVars==1) { alert("ttt"); }
else { alert("eee"); }

The problem you had, in detail:

When there are no query parameters, query would be just the URL. Let me explain step by step. Example page: http://example.com/page

query -> 'http://example.com/page'
vars  -> ['http://example.com/page']
pair  -> ['http://example.com/page']

The problem would be in:

var data1 = pair[1].split('.');

As pair has no item in pair[1] . The code above would be the same as undefined.split('.') , what gives a runtime error, thus preventing the following lines (and the second if you asked) from executing.

What exactly do you mean the statement doesn't work? Neither of the alerts is fired?

I'm guessing pair[1] does not exist and is causing an exception; can you post a sample query ?

The reason the second alert doesn't fire is that query is undefined when hasVars !== 1 . This causes query to be undefined , which means it is not a string and doesn't have string methods (like split ). Attempting to use string methods on a non-string object throws errors, which prevent the rest of your code from running.

To solve this, define query (and the rest of your strings) as:

var query = hasVars ? loc.slice(loc.indexOf('?') + 1) : '';
var vars = hasVars ? query.split('&') : '';
var pair = hasVars ? vars[0].split('=') : '';
var data1 = hasVars ? pair[1].split('.') : '';

This uses the ternary operator ( ?: ) to initialize query as an empty string if hasVars is not 1 .

As query (and the rest of the strings) is now defined as a string one way or the other, no errors are thrown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM