最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - window.location.replace not working if the same page is reloaded - Stack Overflow

programmeradmin1浏览0评论

I'm trying to refresh page on Ajax success.

var lastId = "#" + id;
window.location.replace("/myurl" + lastId);

It is writing the correct URL to my browser but I don't see the changes made. If I refresh (F5) the page, I see the changes correctly because the ajax was correctly sended.

So I think that page won't force refresh if the url is the same.

In this example, I'm already on , it is supossed to redirect me to for example, so my browser will focus the DOM element with id="38174".

As I said, if I submit the ajax request, my url changes to and focus id="38174" but I don't see the changes made to that element on my DB, if I hit F5, it focus the same element but with the changes correctly shown.

Why is this hapening?

I've also tried with window.location.href and window.location without success.

If I use window.location.replace(''); it's sending me to this website correctly...

So I think the problem is when replacing with the same url + some hashtag... maybe?

As I've said, in this case in particular we need to refresh after an AJAX request only if it's a success, yes, it sounds weird and counterproductive but it is needed.

I'm trying to refresh page on Ajax success.

var lastId = "#" + id;
window.location.replace("/myurl" + lastId);

It is writing the correct URL to my browser but I don't see the changes made. If I refresh (F5) the page, I see the changes correctly because the ajax was correctly sended.

So I think that page won't force refresh if the url is the same.

In this example, I'm already on http://mypage./myurl, it is supossed to redirect me to http://mypage./myurl#38174 for example, so my browser will focus the DOM element with id="38174".

As I said, if I submit the ajax request, my url changes to http://mypage./myurl#38174 and focus id="38174" but I don't see the changes made to that element on my DB, if I hit F5, it focus the same element but with the changes correctly shown.

Why is this hapening?

I've also tried with window.location.href and window.location without success.

If I use window.location.replace('http://stackoverflow.'); it's sending me to this website correctly...

So I think the problem is when replacing with the same url + some hashtag... maybe?

As I've said, in this case in particular we need to refresh after an AJAX request only if it's a success, yes, it sounds weird and counterproductive but it is needed.

Share Improve this question edited Nov 23, 2015 at 16:12 Robert W. Hunter asked Nov 23, 2015 at 16:06 Robert W. HunterRobert W. Hunter 3,0037 gold badges36 silver badges74 bronze badges 6
  • 6 Updating the fragment of a URL doesn't cause the page to reload - as you've seen. You would need to change the fragment, then call window.location.reload() manually, however this makes the point of making an AJAX request rather moot. The general idea is that you make the AJAX request, which updates your datastore and then passes back enough information for the UI to update itself. – Rory McCrossan Commented Nov 23, 2015 at 16:07
  • 1 Why are you forcing a reload for an ajax call? That seems like the opposite of what an ajax call is supposed to acplish – Sterling Archer Commented Nov 23, 2015 at 16:08
  • 2 Isn't the point of using Ajax is that you don't need to reload? – Jono20201 Commented Nov 23, 2015 at 16:08
  • Yes, the point of ajax is that you don't need to reload, but in this case in particular we need to reload ONLY if everything went OK, because we're using AngularJS and we don't/can't replace HTML content with the retrieved data, because we are using static content with TWIG, long history, but we need to reload in this case in particular. – Robert W. Hunter Commented Nov 23, 2015 at 16:10
  • @RoryMcCrossan I will need to find another way then, because the fragment will always be the same... – Robert W. Hunter Commented Nov 23, 2015 at 16:12
 |  Show 1 more ment

2 Answers 2

Reset to default 5
var lastId = "#" + id;
window.location.replace("/myurl" + lastId);
window.location.reload();

Updating a hash from the URL will not trigger a new pageload.

You'll need to listen to the hashchange event in javascript to initiate your ajax call, without the need for your page to refresh. This will give you a better user experience

If you use jQuery, you can use something like:

$(window).on('hashchange',function(){ 
    //Do ajax call here
});
发布评论

评论列表(0)

  1. 暂无评论