admin管理员组

文章数量:1335624

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

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

Share Improve this question edited May 10, 2012 at 22:49 Filippo oretti asked May 10, 2012 at 22:04 Filippo orettiFilippo oretti 49.9k96 gold badges229 silver badges351 bronze badges 9
  • So you just want it to be /users/90, or nothing after /users/? – Sampson Commented May 10, 2012 at 22:06
  • @JonathanSampson i need just /users/ – Filippo oretti Commented May 10, 2012 at 22:07
  • but not as string i need to do that on browser bar directly – Filippo oretti 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. – Amadan 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) – Amadan Commented May 10, 2012 at 22:20
 |  Show 4 more ments

5 Answers 5

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

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] );
}

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

http://jsfiddle/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>

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.

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>

本文标签: javascriptJSHTML5 remove url params from urlStack Overflow