Filippo oretti
Filippo oretti

Reputation: 49817

JS/HTML5 remove url params from url

i have an url like this

/users/?i=0&p=90

how can i remove in js the part from

? to 90

can any one show me some code?

EDIT

i mean doing this with window.location.href (so in browser url bar directly)

i tryed

function removeParamsFromBrowserURL(){
    document.location.href =  transform(document.location.href.split("?")[0]);
    return document.location.href;
}

also i would like to not make redirect, so just clean the url from ? to end

Upvotes: 1

Views: 4615

Answers (5)

Alexander
Alexander

Reputation: 1

I had problems with #page back and forth referrals sticking in the url no matter which url redirect I used. This solved everything.

I used the script like this:

<script type="text/javascript">

function strip() {
  whole=document.location.href;
  leftside = whole.split('#')[0];
  document.location.href=leftside;
}
</script>
<a onclick="strip()" href="http://[mysite]/hent.asp" >Click here</a>

Upvotes: 0

Danilo Valente
Danilo Valente

Reputation: 11342

function removeParamsFromBrowserURL(){
    return window.location.href.replace(/\?.*/,'');
}

Upvotes: 4

Tina CG Hoehr
Tina CG Hoehr

Reputation: 6779

One way is leftside = whole.split('?')[0], assuming there's no ? in the desired left side

http://jsfiddle.net/wjG5U/1/

This will remove ?... from the url and automatically reload the browser to the stripped url (can't get it to work in JSFiddle) I have the code below in a file, and put some ?a=b content manually then clicked the button.

<html>
  <head>
    <script type="text/javascript">
function strip() {
  whole=document.location.href;
  leftside = whole.split('?')[0];
  document.location.href=leftside;
}

    </script>
  </head>
  <body>
    <button onclick="strip()">Click</button>
  </body>
</html>

Upvotes: 1

Sampson
Sampson

Reputation: 268324

If you only want the /users/ portion:

var newLoc = location.href.replace( /\?.+$/, '' );

You could also split the string, and return the first portion:

var newLoc = location.href.split("?")[0];

Or you could match everything up to the question mark:

if ( matches = location.href.match( /^(.+)\?/ ) ) {
  alert( matches[1] );
}

Upvotes: 2

BDFun
BDFun

Reputation: 551

If you only want the /users/ portion, then you could just substring it:

var url = users/?i=0&p=90;
var urlWithNoParams = url.substring(0, url.indexOf('?') - 1);

That extracts the string from index 0 to the character just before the '?' character.

Upvotes: 0

Related Questions