找出如果JS-ASP存在URL参数 [英] Finding out if a URL param exists in JS-ASP

查看:112
本文介绍了找出如果JS-ASP存在URL参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编辑其他人的code,写在服务器端JS的ASP,并遇到了大概有一个非常简单的解决方案的一个问题。

I am editing other people's code, written in server-side JS for ASP, and have run into a problem that probably has a very simple solution.

我是从一个URL参数像这样的一些输出code

I'm outputting some code from a URL param like this:

<%=Request.QueryString("param")%>

问题是,如果参数不存在,我需要做些别的事情。所以我尝试:

The problem is that if the param doesn't exist, I need to do something else. So I tried:

<% 
  var param = Request.QueryString("param");
  if (!param) { param = "Some Default Value"; }
%>
<%=param%>

问题是,如果似乎永远不会计算为真正,即使在URL参数丢失。我猜测,在!图片状态不在这里工作了。我应该在我的测试条件是什么?

The problem is that the if never seems to evaluate to true, even when the URL param is missing. I'm guessing that the !image condition doesn't work here. What should my test condition be?

(请放弃这样做网址参数的转义为prevent XSS严厉的警告。)

(Please forgo stern warnings about doing escaping of URL params to prevent XSS.)

推荐答案

检查查询字符串参数是否存在正确的方法是用计数属性:

The correct way to check whether a query string parameter exists is with the Count property:

<% 
  var param = Request.QueryString("param");
  if (param.Count === 0) { param = "Some Default Value"; }
%>
<%=param%>

据为 <$ c中的文档$ C>的Request.QueryString

的Request.QueryString的值(参数的)是所有的数组
  参数值出现在QUERY_STRING。

The value of Request.QueryString(parameter) is an array of all of the values of parameter that occur in QUERY_STRING.

这可能是为什么简单的如果(!参数)检查不能正常工作。

That's probably why the simple if (!param) check doesn't work.

这篇关于找出如果JS-ASP存在URL参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆