简体   繁体   English

我如何设置查询字符串是否确实不是

[英]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.

相关问题 使用滚动捕捉时,如何将默认位置设置为第一列或第一行以外的位置? - When using scroll snap, how can I set the default position to something other than the first column or row? 我可以将Dojo名称空间更改为dojo以外的名称吗? - Can I change the Dojo namespace to something other than dojo? 如何检测用户何时触摸菜单或其子女以外的其他内容? - How can I detect when a user touches something other than a menu or it's children? 我怎样才能让一半的玩家站在另一边? - How can I make half the player come out on the other side? 我怎样才能制作一个像 jQuery 的 slideUp() 一样的 function,但是对于向上以外的方向? - How can I make a function that acts like jQuery's slideUp(), but for directions other than up? 我该如何设置一个函数,而不是直接调用它 - How can I set up a function so it's called rather than inline 我可以使用$以外的其他方式来防止jQuery插件之间发生冲突吗? - Can I use something other than $ to prevent conflict between jQuery plugins? 我可以使用输入类型=“提交”以外的其他方式发送表单吗? - Can I use something other than an input type=“submit” to send a form? 如何设置观察程序以将文件复制到同一文件夹中的其他文件? - How can I set up a watcher to copy files to other files inside same folders? 如何检查字符串是否包含某个字母,后跟除自身或元音以外的任何字母? - How can I check if string contains a certain letter followed by anything other than itself or vowels?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM