【问题标题】:javascript Request.QueryStringjavascript Request.QueryString
【发布时间】:2011-06-10 08:56:35
【问题描述】:

如何使用 javascript 从 URL 请求查询字符串

例如:http://localhost:1247/portal/alias__MySite/lang__en/tabid__3381/default.aspx

我想要tabid...

 var tabid = '<%= Request.QueryString["tabid"] %> ';

以上代码仅适用于 aspx 页面 但我不需要它,有什么想法吗?谢谢

【问题讨论】:

  • 正如我在回答中所写,我怀疑您是否在寻找 QueryString,因为服务器上有重写机制。如果要获取数字,则需要解析URL。

标签: c# javascript asp.net query-string


【解决方案1】:

现在有一个新的 api URLSearchParams。与window.location.search结合使用

var urlParams = new URLSearchParams(window.location.search);
console.log(urlParams.get('tabid'));

如果您的浏览器不支持URLSearchParams,您可以创建自定义回退函数:

function getParam(name) {
    name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
    var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
    var results = regex.exec(location.search);
    return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
console.log(getParam('tabid'));

【讨论】:

    【解决方案2】:

    不知道为什么,但我总是发现用于获取查询字符串数据的 javascript 有点 hacky。如果您在初始页面加载时不需要此值,那么也许您可以在代码中使用 Request.QueryString 并将值设置为隐藏字段,您的 javascript 将从该字段中读取?

    【讨论】:

      【解决方案3】:

      试试这个,它非常适合我。

      function getParameterByName(name) {
         name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
         var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
          results = regex.exec(location.search);
         return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
      }
      
      var tabId=getParameterByName("tabid");
      

      【讨论】:

        【解决方案4】:

        我敢打赌有一个服务器端重写(DotNetNuke?),所以 aspx.cs “看到”包含正确 QueryString 的重定向目标。

        对于客户端,您必须使用另一种机制,因为浏览器只能“看到”公共 URL。在这种情况下,选择“tabid_”后面和下一个斜杠之前的数字的正则表达式应该可以工作。这将是 aspx 页面“看到”的相同编号(页面 id?)。

        【讨论】:

          【解决方案5】:

          这是我用的:

          <script type="text/javascript">   
          
          function QueryString(key) {  
          //Get the full querystring  
          fullQs = window.location.search.substring(1);  
          //Break it down into an array of name-value pairs  
          qsParamsArray = fullQs.split("&");  
          //Loop through each name-value pair and   
          //return value in there is a match for the given key  
          for (i=0;i<qsParamsArray.length;i++) {  
          strKey = qsParamsArray[i].split("=");  
              if (strKey[0] == key) {  
                  return strKey[1];  
              }  
          }  
          }  
          
          //Test the output (Add ?fname=Cheese&lname=Pizza to your URL)  
          //You can change the variable to whatever it is you need to do for example, you could  
          //change firstname to id and lastname to userid and just change the reference in the
          //document.write/alert box
          var firstname = QueryString("fname"); 
          var lastname = QueryString("lname");   
          document.write("You are now logged in as " + firstname + " " + lastname + "!");  
          
          </script>
          

          您可以将 document.write 替换为 alert,它会给您一个警告框!

          我在我的网站上使用了这个。它还没有完成,但它会在 zducttapestuff.com 上完成

          输出将如下所示:您现在以 Cheese Pizza 的身份登录!

          这对于密码来说非常不安全,因为密码会显示在 url 中。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多