[英]How can I set an if the query string is does come up as something other than
How can I make a javascript if function where if the query string does show as something other than undefined. 如何在查询字符串确实显示为未定义之外的其他地方制作javascript if函数。 Here is the code: 这是代码:
var getQueryString = window.location.href.split("?")[1];
alert(getQueryString);
Basically I wanted to do an if function in javascript. 基本上我想在JavaScript中执行if函数。 Here is the code: 这是代码:
if (getQueryString = 1){
//add code
}
How can I set my javascript function? 如何设置我的JavaScript函数?
I would use location.search
and check it's length
instead of your approach. 我将使用location.search
并检查其length
而不是您的方法。
if (window.location.search.slice(1).length > 0) {
doSomething();
}
Currently, your code will always execute because you're setting getQueryString
to 1 instead of comparing it. 当前,您的代码将始终执行,因为您将getQueryString
设置为1而不是对其进行比较。 Even if you were comparing it, it would still be false since getQueryString
is a string and not a number. 即使您进行比较,由于getQueryString
是一个字符串而不是一个数字,它仍将为false。
The bellow code should work for you 以下代码应为您工作
var getQueryString = window.location.href.split("?")[1];
if (typeof getQueryString !== 'undefined')
{
//Your code goes here
} else [
alert("Query String not set");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.