简体   繁体   English

javascript Request.QueryString

[英]javascript Request.QueryString

How do I request querystring using javascript from URL如何使用来自 URL 的 javascript 请求查询字符串

eg: 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?以上代码仅适用于 aspx 页面,但我不需要它,有什么想法吗? thanks谢谢

There is now a new api URLSearchParams .现在有一个新的 api URLSearchParams Use that in conjunction with window.location.search将其与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:

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

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

Don't know why but I've always found the javascript for querystring data fetching a bit hacky.不知道为什么,但我总是发现 javascript 用于获取查询字符串数据有点 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?如果您在初始页面加载时不需要此值,那么也许您可以在代码中使用 Request.QueryString 并将值设置为隐藏字段,您的 javascript 将从中读取?

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它还没有完成,但它会在 zducttapestuff.com

The output will look like this: You are now logged in as Cheese Pizza! output 将如下所示: 您现在以 Cheese Pizza 的身份登录!

This is very unsecure for Passwords though since the password will be shown in the url.这对于密码来说是非常不安全的,因为密码将显示在 url 中。

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.对于客户端,您必须使用另一种机制,因为浏览器只能“看到”公共 URL。 In this case, a Regex that picks the number behind 'tabid_' and before the next slash should work.在这种情况下,选择“tabid_”后面和下一个斜杠之前的数字的正则表达式应该可以工作。 This would be the same number (page id?) that the aspx page "sees".这将是 aspx 页面“看到”的相同数字(页面 id?)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM