admin管理员组

文章数量:1410674

I'm trying to link to an inner page anchor and force a refresh of the page.

However this does not force a refresh:

<a href="/#example">example link</a>

What is the best method of linking to an anchor in the same page AND forcing a refresh?

Also I would like the refresh to be a GET request, even if the page was last rendered with a POST request.

I'm trying to link to an inner page anchor and force a refresh of the page.

However this does not force a refresh:

<a href="/#example">example link</a>

What is the best method of linking to an anchor in the same page AND forcing a refresh?

Also I would like the refresh to be a GET request, even if the page was last rendered with a POST request.

Share Improve this question edited Nov 7, 2015 at 23:02 Shikkediel 5,20516 gold badges49 silver badges79 bronze badges asked Nov 7, 2015 at 18:51 HardlyNoticeableHardlyNoticeable 5179 silver badges27 bronze badges 5
  • Are you trying to move page scroll to an element on page load? <a href="somepageurl.html#someelementid">example link</a> – AtheistP3ace Commented Nov 7, 2015 at 18:57
  • @AtheistP3ace: Please read my question; I also want to link within the page AND force a page refresh. – HardlyNoticeable Commented Nov 7, 2015 at 20:01
  • @HardlyNoticeable yes what I put in ment will load the url and go to the element in page. If the url is the same page you are on it essentially reloads the page. – AtheistP3ace Commented Nov 7, 2015 at 20:41
  • I don't know why people downvoted this question. Web application developers do a lot of URL rewriting now-a-days and this is exactly the kind of issue one may experience when bining URL rewriting with Inner Page Links. – HardlyNoticeable Commented Nov 12, 2015 at 1:09
  • 2 I hear you. I didn't down vote you. But it drives me nuts when people down vote something and leave no reason why. We can all learn from it possibly. – AtheistP3ace Commented Nov 12, 2015 at 1:12
Add a ment  | 

2 Answers 2

Reset to default 5

Add JS onClick="window.location.reload()" event as well to your a tag, full example:

<a href="#example" onClick="window.location.reload()">Click to refresh</a>

There are a ton of ways to refresh a page with JS, just tie it to your click event. Here are some examples,

location = location
location = location.href
location = window.location
location = self.location
location = window.location.href
location = self.location.href
location = location['href']
location = window['location']
location = window['location'].href
location = window['location']['href']
location = window.location['href']
location = self['location']
location = self['location'].href
location = self['location']['href']
location = self.location['href']
location.assign(location)
location.replace(location)
window.location.assign(location)
window.location.replace(location)
self.location.assign(location)

Taken from 535 ways to reload a page with JS.

In the end, the only thing that seemed to work for me, was to use a bination of window.location and adding a parameter. So what I ended up doing looked like this:

    <p><a href="#example" id="example">Click Here</a></p>

and then the following jQuery:

    $('#example').click(function(event) {

        window.location = '/?anything#example';

    });

I tried adding the parameter directly to the link; but that didn't force a page load; only when adding it using window.location did it seem to work.

本文标签: javascriptForce page refresh on inner page linkStack Overflow