jQuery 删除一个 URL 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7126563/
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-08-26 23:25:57  来源:igfitidea点击:

jQuery remove one URL variable

jqueryurlparameters

提问by sn0ep

I am busy making a web application, which needs to process a lot of erorrs and stuff so i made a jquery ui dialog box. Which shows these errors. The errors are retrieved by database. for example:

我正在忙于制作一个 Web 应用程序,它需要处理很多错误和东西,所以我制作了一个 jquery ui 对话框。这显示了这些错误。错误由数据库检索。例如:

when my username/password is incorrect when i try to log in i get redirected to

当我尝试登录时我的用户名/密码不正确时,我被重定向到

http://domain.com/?do=login&e=login-incorrect

the application then know he has to search for the login-incorrect error in the database and show that to the user. Now this all goes very well. Except that when for some reason the user would reload this particular page he wel get the error message again while he doesnt need to get it.

然后应用程序知道他必须在数据库中搜索登录不正确的错误并将其显示给用户。现在这一切都很顺利。除了当用户由于某种原因重新加载这个特定页面时,他会再次收到错误消息,而他不需要得到它。

so my plan is to bind some kind of function to the closeevent of the dialog box and redirect the user to the same page bug without the e parameter in the URL. How could i achieve this. I tried all sorts of stuff. But could not get it to work.

所以我的计划是将某种函数绑定到close对话框的事件,并将用户重定向到相同的页面错误,而 URL 中没有 e 参数。我怎么能做到这一点。我尝试了各种各样的东西。但无法让它工作。

What i tried:

我试过的:

bassicly tried getting all possible parameters and stitching those together except for the e parameter. like so:

基本上尝试获取所有可能的参数并将它们拼接在一起,除了 e 参数。像这样:

$ERROR = $_GET['e'];
$DO = $_GET['do'];
$P = $_GET['p'];
$C = $_GET['c'];
$T = $_GET['t'];
$ACTION = $_GET['action'];

// URL WITHOUT ERRORS
$needP = "";
$needACTION = "";
$needDO = "";
if($P != ""){
    $needP = "p=".$P."&";
}
if($DO != ""){
    $needDO = "do=".$DO."&";
}
if($ACTION != ""){
    $needACTION = "action=".$ACTION."";
}
$NOERRORURL = $BASEURL."?".$needP.$needDO.$needACTION;

But it does not work and its ugly

但它不起作用而且很丑

回答by jli

location.href=location.href.replace(/&?e=([^&]$|[^&]*)/i, "");

This will remove all instances of the e parameter from the query string and refresh the page.

这将从查询字符串中删除 e 参数的所有实例并刷新页面。

回答by Jens Roland

One way to do it:

一种方法:

location.search = location.search.replace(/&e=[^&;]*/,'');

That will reload the page like you mention, without the 'e' parameter

这将像你提到的那样重新加载页面,没有“e”参数



However, a much better approach is to use a hash instead, like this: http://domain.com/?do=login#login-incorrect. Then you can check for the hash when the page loads, and open your error dialog if the login-incorrecthash is found:

然而,更好的方法是使用哈希代替,就像这样:http://domain.com/?do=login#login-incorrect。然后您可以在页面加载时检查哈希,如果login-incorrect找到哈希,则打开错误对话框:

if (location.hash.indexOf('login-incorrect') != -1) {
  // Open your jQuery UI dialog here
}

And if they close the dialog, you can simply clear the hash without having to refresh the page:

如果他们关闭对话框,您可以简单地清除哈希而无需刷新页面:

$('.closeErrorDialog').click(function(){
  location.hash = '';
});

If you are going to have a lot of these, I recommend Ben Alman's jQuery hashchange eventor the more full-featured BBQ pluginwhich make your life a lot easier when you are working with hashes.

如果你打算拥有很多这些,我推荐 Ben Alman 的jQuery hashchange 事件或更全功能的BBQ 插件,当你使用哈希时,它会让你的生活更轻松。

回答by Maxim Maslov

urlObject = new URL(url);
urlObject.searchParams.delete('myParameter');

The Url Object has a searchParams attribute which has the function delete. This will remove myParameterfrom the urlObject. If you want to access your url again, just:

Url 对象有一个具有删除功能的 searchParams 属性。这将删除myParameter从urlObject。如果您想再次访问您的网址,只需:

url = urlObject.href;

Further:

更远:

https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/deletehttps://developer.mozilla.org/de/docs/Web/API/URL/searchParams

https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/delete https://developer.mozilla.org/de/docs/Web/API/URL/searchParams