Finding out if a URL param exists in JS-ASP

家住魔仙堡 提交于 2019-12-01 13:29:23

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%>

According to the documentation for Request.QueryString,

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.

This is what I do.

function qs(name) {
    var v = Request.QueryString(name),
        v2 = "x" + v + "x";
    if ((v2=="xundefinedx") && (v != "undefined")) {
        return null;
    }
    return v + ''; // force string
}

In JSP you have to use getParameter instead of QueryString

The code in JSP would be

<% 
  String param = request.getParameter("param");
  if (param.length() == 0) { param = "Some Default Value"; }
%>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!