1

I have this URL and wanting to know how can I remove this section from it via a jQuery event.

Need to remove:

&activities_id=13&session_id=14&back=1

Original URL:

http://somedomain.com/ijob-css/index.php/search/default/index/area/act?query=&activities_id=13&session_id=14&back=1

EDIT

Sorry i think i havent included the most important section. I should change the Address BAR url not a normal string.

for example, if i have this url in the address bar - http://somedomain.com/ijob-css/index.php/ after change, address bar should contain http://somedomain.com/xxx=111, without page refreshing.

dev1234
  • 5,376
  • 15
  • 56
  • 115
  • 2
    You could remove this from a string which was assigned the URI but is that what you want? Or do you want to actually point the browser to a new URI minus the parts you want to remove? – andrewb Sep 17 '13 at 04:54
  • @andrew-buchan i want to remove the mentioned section from the URL and leave the page without refreshing. its like this, once user presses aback button he is taken here to a show a preloaded dialog box. so after showing the dialog box i need to remove that portion from URL without page is being reloaded. – dev1234 Sep 17 '13 at 04:56
  • Check out the [link](https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history?redirectlocale=en-US&redirectslug=Web%2FGuide%2FDOM%2FManipulating_the_browser_history) from my answer below. I believe replaceState() is the answer for your problem. However it is not supported in all browsers/versions. History.js wraps HTML5 state features and provides additional support for HTML4 browsers. – Sanjeevi Rajagopalan Sep 17 '13 at 05:07
  • possible duplicate of [Modify the URL without reloading the page](http://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page) – davidkonrad Sep 17 '13 at 05:17

6 Answers6

2

Do you mean you want the URL without the query parameter part? If then see if this helps.

var test = 'http://somedomain.com/ijob-css/index.php/search/default/index/area/act?query=&activities_id=13&session_id=14&back=1';

alert(test.substring(0, test.indexOf('?')));

If you want until first query parameter name then just seek until index of &

Update :

If you are using HTML5 then what you ask is possible. Check browser history manipulation. You can find details about this here.

I believe replaceState() is the answer for your problem. However it is not supported in all browsers/versions. History.js wraps HTML5 state features and provides additional support for HTML4 browsers.

  • Yes. This is doable. Check [this](http://spoiledmilk.com/blog/html5-changing-the-browser-url-without-refreshing-page/) link out. It explains the steps in detail. Also check out the example link in that page. It changes the URL everytime to fetch a photo without page reload. – Sanjeevi Rajagopalan Sep 17 '13 at 05:14
1

Try this out

var new_url = old_url.substring(0, old_url.indexOf('&'));

Example: http://jsfiddle.net/SjrqF/

var url = 'youtube.com/watch?v=3sZOD3xKL0Y&feature=youtube_gdata';

url = url.slice( 0, url.indexOf('&') );

or:

Example: http://jsfiddle.net/SjrqF/1/

var url = 'youtube.com/watch?v=3sZOD3xKL0Y&feature=youtube_gdata';

url = url.split( '&' )[0];
Shareek Ahamed
  • 378
  • 1
  • 13
1
var lastPart = 'query=';
var url = 'http://somedomain.com/ijob-css/index.php/search/default/index/area/act?query=&activities_id=13&session_id=14&back=1'.split(lastPart)[0] + lastPart;
Trevor Dixon
  • 23,216
  • 12
  • 72
  • 109
1
var index = original_url.indexOf("=");
var new_url = original_url.substring(0,index+1);
Alex Art.
  • 8,711
  • 3
  • 29
  • 47
1

See below.

var positionToSubstring = this.location.href.indexOf('&');
var newURI = this.location.href.substring(0, positionToSubstring);
andrewb
  • 2,995
  • 7
  • 54
  • 95
1

use this

var test='http://somedomain.com/ijob-css/index.php/search/default/index/area/act?query=&activities_id=13&session_id=14&back=1';
test=test.split('&')[0];

console.log(test);

outputs

http://somedomain.com/ijob-css/index.php/search/default/index/area/act?query=

davidkonrad
  • 83,997
  • 17
  • 205
  • 265