Finding out if a URL param exists in JS-ASP(查明 JS-ASP 中是否存在 URL 参数)
问题描述
我正在编辑其他人的代码,用 ASP 的服务器端 JS 编写,遇到了一个问题,可能有一个非常简单的解决方案.
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 参数中输出一些代码:
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%>
问题是 if
似乎永远不会评估为 true
,即使 URL 参数丢失也是如此.我猜 !image
条件在这里不起作用.我的测试条件应该是什么?
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?
(请放弃关于转义 URL 参数以防止 XSS 的严厉警告.)
(Please forgo stern warnings about doing escaping of URL params to prevent XSS.)
推荐答案
检查查询字符串参数是否存在的正确方法是使用 Count
属性:
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%>
根据 的文档Request.QueryString
,
Request.QueryString(parameter) 的值是一个包含所有QUERY_STRING 中出现的参数值.
The value of Request.QueryString(parameter) is an array of all of the values of parameter that occur in QUERY_STRING.
这可能是简单的 if (!param)
检查不起作用的原因.
That's probably why the simple if (!param)
check doesn't work.
这篇关于查明 JS-ASP 中是否存在 URL 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:查明 JS-ASP 中是否存在 URL 参数


基础教程推荐
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01