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

9
  • So you just want it to be /users/90, or nothing after /users/? Commented May 10, 2012 at 22:06
  • @JonathanSampson i need just /users/ Commented May 10, 2012 at 22:07
  • but not as string i need to do that on browser bar directly Commented May 10, 2012 at 22:08
  • 1
    document.location.href = transform(document.location.href); use any of the below answers for the transform itself. Commented May 10, 2012 at 22:09
  • 1
    Errm... I meant more something along the lines of document.location.href = document.location.href.split("?")[0] (with transform being a metaphorical function indicating one of the answers below - it does not exist) Commented May 10, 2012 at 22:20

5 Answers 5

4
function removeParamsFromBrowserURL(){
    return window.location.href.replace(/\?.*/,'');
}
Sign up to request clarification or add additional context in comments.

3 Comments

should be ok but it redirects me
Of course it redirects. There is no way to do this without the redirect.
No, this function just returns the "clean" part of the window.lcoation.href
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] );
}

Comments

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>

1 Comment

I'm no expert, but I believe these guys :) stackoverflow.com/a/6478682/1325290 no way without redirecting :(
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.

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.