jQuery 删除一个 URL 变量

发布于 2024-11-30 18:08:18 字数 912 浏览 0 评论 0原文

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

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

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

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

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

我尝试过的:

基本上尝试获取所有可能的参数并将它们缝合在一起(除了 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;

但是它不起作用而且很丑

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:

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 close event 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.

What i tried:

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

$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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不必了 2024-12-07 18:08:18
location.href=location.href.replace(/&?e=([^&]$|[^&]*)/i, "");

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

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

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

山人契 2024-12-07 18:08:18

一种方法是:

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

这将像您提到的那样重新加载页面,而不需要“e”参数


但是,更好的方法是使用散列,如下所示:http://domain.com/?do =登录#登录错误。然后,您可以在页面加载时检查哈希值,如果发现 login-in Correct 哈希值,则打开错误对话框:

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

如果他们关闭对话框,您只需清除哈希值即可,而无需刷新页面:

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

如果您想要很多这样的内容,我推荐 Ben Alman 的 jQuery hashchange 事件< /a> 或更多功能齐全的 BBQ 插件,当您使用哈希时,它会让您的生活变得更加轻松。

One way to do it:

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

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


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-incorrect hash is found:

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 event or the more full-featured BBQ plugin which make your life a lot easier when you are working with hashes.

撩起发的微风 2024-12-07 18:08:18
urlObject = new URL(url);
urlObject.searchParams.delete('myParameter');

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

url = urlObject.href;

进一步:

https ://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/delete
https://developer.mozilla.org/de/docs/Web/ API/URL/搜索参数

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

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

url = urlObject.href;

Further:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文