javascript Request.QueryString

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6304132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:15:09  来源:igfitidea点击:

javascript Request.QueryString

c#javascriptasp.netquery-string

提问by progproger

How do I request querystring using javascript from URL

如何使用来自 URL 的 javascript 请求查询字符串

e.g : http://localhost:1247/portal/alias__MySite/lang__en/tabid__3381/default.aspx

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

I want to get tabid...

我想...

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

Above code works only in aspx page but i dont need it, any ideas? thanks

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

回答by Matt Spinks

There is now a new api URLSearchParams. Use that in conjunction with window.location.search

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

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

If your browser does not support URLSearchParams, you can create a custom fallback function:

如果您的浏览器不支持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'));

回答by Amit

Try this, It is working perfectly for me.

试试这个,它对我来说完美无缺。

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");

回答by Грозный

Don't know why but I've always found the javascript for querystring data fetching a bit hacky. if you don't need this value on the initial page load then perhaps you could use Request.QueryString in the code and set the value to a hidden field, which your javascript will read from?

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

回答by Zach

This is what I used:

这是我使用的:

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

You can replace document.write with alert and it would give you an alert box instead!

你可以用 alert 替换 document.write ,它会给你一个警告框!

I used this on my website. Its not done yet but when it is it will be at zducttapestuff.com

我在我的网站上使用了这个。它尚未完成,但当它完成时将在 zducttapesuff.com

The output will look like this: You are now logged in as Cheese Pizza!

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

This is very unsecure for Passwords though since the password will be shown in the url.

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

回答by Olaf

I bet there is a server-side rewrite (DotNetNuke?), so the aspx.cs "sees" the redirection target which contains the correct QueryString.

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

For the client, you have to use another mechanism because the browser only "sees" the public URL. In this case, a Regex that picks the number behind 'tabid_' and before the next slash should work. This would be the same number (page id?) that the aspx page "sees".

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