sn0ep
sn0ep

Reputation: 3943

jQuery remove one URL variable

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

Upvotes: 17

Views: 40607

Answers (3)

TheMasterM
TheMasterM

Reputation: 71

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

Upvotes: 4

Jens Roland
Jens Roland

Reputation: 27770

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.

Upvotes: 4

jli
jli

Reputation: 6623

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

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

Upvotes: 34

Related Questions