使用javascript将用户重定向到当前页面并使用一些查询字符串
当某人点击某个链接时,我想这样做:
When a person clicks on a link, I want to do this:
- 从文本框中抓取文字
- 编码文本
- 重定向到currentpage.aspx?search = textboxvalue
I have this so far but its not working:
window.location.href = "?search=" + escape( $("#someId").val());
我需要做类似于你想要的事情我整理了一个允许你这样做的功能。我希望它有用。我已经对它进行了相当广泛的测试,没有发现任何问题,如果发现任何问题,请随时告诉我。
I needed to do something similar to what you wanted so I put together a function that allows you to do it. I hope it is found useful. I have tested it pretty extensively and not found any issues, please don't hesitate to let me know if any issues are found.
function reloadWithQueryStringVars (queryStringVars) {
var existingQueryVars = location.search ? location.search.substring(1).split("&") : [],
currentUrl = location.search ? location.href.replace(location.search,"") : location.href,
newQueryVars = {},
newUrl = currentUrl + "?";
if(existingQueryVars.length > 0) {
for (var i = 0; i < existingQueryVars.length; i++) {
var pair = existingQueryVars[i].split("=");
newQueryVars[pair[0]] = pair[1];
}
}
if(queryStringVars) {
for (var queryStringVar in queryStringVars) {
newQueryVars[queryStringVar] = queryStringVars[queryStringVar];
}
}
if(newQueryVars) {
for (var newQueryVar in newQueryVars) {
newUrl += newQueryVar + "=" + newQueryVars[newQueryVar] + "&";
}
newUrl = newUrl.substring(0, newUrl.length-1);
window.location.href = newUrl;
} else {
window.location.href = location.href;
}
}
如何使用
该函数接受一个对象文字作为其唯一参数。对象文字应包含要重新加载页面的查询字符串变量。如果你在传递的对象文字中指定现有变量,它会占用现有变量并覆盖现有变量。
How to Use It
The function accepts an object literal as its only argument. The object literal should contain the query string variable(s) that you want to reload the page with. It accounts for existing variables and will override an existing one if you specify it in the object literal you pass.
所以,假设我们的位置是 http://localhost/helloworld.php
我希望使用查询字符串变量 foo
重定向到当前页面,其值应为 bar
,我将按如下方式调用该函数:
So, lets say our location is http://localhost/helloworld.php
and I want to redirect to the current page with a query string variable foo
whose value should be bar
, I'd call the function as follows:
reloadWithQueryStringVars({"foo": "bar"});
浏览器将导航到 http://localhost/helloworld.php?富=栏
。如果我传递以下函数:
The browser will navigate to http://localhost/helloworld.php?foo=bar
. And If I pass the function the following:
reloadWithQueryStringVars({"foo": "bar2"});
浏览器将导航到 http://localhost/helloworld.php? foo = bar2
。
当函数接受对象文字时,您可以将多个属性传递给函数以获取多个查询字符串变量。如果我仍然在 http://localhost/helloworld.php?foo = bar2
,我将函数调用如下
As the function accepts a object literal, you can pass multiple properties to the function for multiple query string vars. If I'm still at http://localhost/helloworld.php?foo=bar2
and I call function as follows
reloadWithQueryStringVars({"foo": "bar3", "answer": "42", "bacon": "tasty"});
浏览器将导航到 http://localhost/helloworld.php? foo = bar3& answer = 42& bacon = delicious
但是在回答这个问题时,你可以按如下方式调用函数:
In answer to the question however, you'd call the function as follows:
reloadWithQueryStringVars({"search": escape( $("#someId").val() )});