問題描述
我正在編輯其他人的代碼,用 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 參數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!