0

I use location.relaod() to reload the current page (from the cache or not). This reloads the page as it - with all GET parameters in the URL.

If the current page is https://www.example.com/somewhere/page.html?a=3&b=4, is it possible to trigger the reload of https://www.example.com/somewhere/page.html?

The current solution I plan to use is to

var fullURL = "https://www.example.com/somewhere/page.html?a=3&b=4"
var splitURL = fullURL.split('?')[0]
document.write(splitURL)

but maybe a more Javascripthic way is already implemented?

Javascriphic was borrowed from from Python -> Pythonic

WoJ
  • 27,165
  • 48
  • 180
  • 345
  • Im confused - your title says 'without its parameter' but in the question you say `is it possible to trigger the reload of https://www.example.com/somewhere/page.html?a=3&b=4` which contains all of the parameters. What do you wanna do? – Xatenev Jun 27 '17 at 11:08
  • 2
    Possible duplicate of [How to get the URL without any parameters in JavaScript?](https://stackoverflow.com/questions/6257463/how-to-get-the-url-without-any-parameters-in-javascript) – Andreas Jun 27 '17 at 11:08
  • @Xatenev: sorry, I started to clarify the question when I realized that I forgot to remove the parameters in the second URL. Fixed. – WoJ Jun 27 '17 at 11:10

3 Answers3

3

Use the location object

// similar behavior as an HTTP redirect
window.location.replace(location.protocol + '//' + location.host + location.pathname);

// similar behavior as clicking on a link
window.location.href = location.protocol + '//' + location.host + location.pathname;
Xatenev
  • 6,383
  • 3
  • 18
  • 42
  • Is there an avantage of this construction compared to the `split()` I was planning to use? – WoJ Jun 27 '17 at 11:13
  • Probably not. Personally I wouldn't do string manipulation if I don't have to because all the information is already there. But [YMMV](https://en.wiktionary.org/wiki/your_mileage_may_vary) – Xatenev Jun 27 '17 at 11:15
2
window.location=window.location.split("?")[0];

Simply redirect..

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
-2

open the console and take a look at document.location,you can build the url you need from some of those fields,like host+pathname

  • how come? he already knows what he has to do to change the url,i simply told him where he can find something that already did what he manually did with .split – Lorenzo Catalano Jun 27 '17 at 11:14
  • The above has more intend of a comment. Answers are like the ones below... Concrete working code samples that prove to fix his initial problem. – daan.desmedt Jun 27 '17 at 11:27