[英]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 第一个if-else语句按预期工作,当地址栏变量在那里时,它执行if部分,而不是在那里执行其他...但是,在VAR线之后,将所有其他语句拆分为一切不行
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 我评论了每一行并将它们放回来并且问题似乎与data1变量行有关,注释掉它工作正常但是有了else语句不能运行
The var query (...)
part of your code will only work when there are query parameters. 代码的
var query (...)
部分仅在有查询参数时才有效。 So it is having problems when there are none. 所以当没有问题时就会出现问题。 One solution would be to place an
if
there as well. 一种解决方案是在那里放置一个
if
。
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"); }
When there are no query parameters, query
would be just the URL. 当没有查询参数时,
query
将只是URL。 Let me explain step by step. 让我一步一步解释。 Example page:
http://example.com/page
示例页面:
http://example.com/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]
. 由于
pair
在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. 上面的代码与
undefined.split('.')
,它会产生运行时错误,从而阻止执行以下行(以及第二行,如果您要求)。
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; 我猜测
pair[1]
不存在并导致异常; can you post a sample query
? 你可以发布样本
query
吗?
The reason the second alert doesn't fire is that query
is undefined
when hasVars !== 1
. 第二个警报未触发的原因是当
hasVars !== 1
时query
undefined
。 This causes query
to be undefined
, which means it is not a string
and doesn't have string methods (like split
). 这会导致
query
undefined
,这意味着它不是string
,也没有字符串方法(如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: 要解决此问题,请将
query
(以及其余字符串)定义为:
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
. 如果
hasVars
不是1
则使用三元运算符( ?:
hasVars
将query
初始化为空字符串。
As query
(and the rest of the strings) is now defined as a string
one way or the other, no errors are thrown. 由于
query
(以及其余字符串)现在以某种方式定义为string
,因此不会抛出任何错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.