1

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

itsme
  • 48,972
  • 96
  • 224
  • 345

5 Answers5

4
function removeParamsFromBrowserURL(){
    return window.location.href.replace(/\?.*/,'');
}
Danilo Valente
  • 11,270
  • 8
  • 53
  • 67
2

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] );
}
Sampson
  • 265,109
  • 74
  • 539
  • 565
1

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>
Tina CG Hoehr
  • 6,721
  • 6
  • 44
  • 57
0

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.

BDFun
  • 531
  • 2
  • 7
0

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>
Lance Roberts
  • 22,383
  • 32
  • 112
  • 130