22

I want to change the user status on click of a Button, so all I am doing is, detecting the current status and changing, if needed.

But in this case the changes the status in backend, but to show the status the page needs to be refreshed, as on refresh it checks the current status and shows. So I am using the "window.location.reload" property to show the latest status on page

All the things are working fine in IE. But in case of Firefox and Chrome, The status is not changing. I think "window.location.reload" is not working, because when I just comment this line and try clicking the button and manually refresh the page it shows the changes Status.

Can you please suggest what should I use to make this work in Firefox and Chrome?

When I googled, I found somewhere it works in Firefox and Chrome if you give it in "setTimeout()". I tried it, but even then its not working for me.

<script>

    $(document.body).on('click', '#activate', function () {
        var pkgVersion = $(this).attr('data-version');
        alert("@Url.Action("SetActivePackage", "Deployment")", { version: pkgVersion });
        $.get("@Url.Action("SetActivePackage", "Deployment")", { version: pkgVersion }).done(function () {

        });
         setTimeout(function () { window.location.reload(data); }, 0);
    });
</script>

Please suggest!

4

9 回答 9

35

我有另外两个选择给你:

history.go(0);

和:

window.location.href = window.location.href;

我还没有在 Firefox 和 Chrome 上测试过它,但它可以帮助你更快。如果这不起作用,明天我将进行测试并更新答案。

于 2013-09-23T19:58:26.903 回答
33

我在 AngularJS 和 Firefox 中遇到了这个问题。(在 Chrome 中重新加载很好)。通过使用 setTimeout 问题解决了。

setTimeout(function(){
  window.location.reload();
});
于 2015-01-27T13:14:30.270 回答
12

你试过这个吗?:

window.location = window.location;

显然你可以放弃“窗口。”,但我自己没有尝试过。

于 2013-09-23T19:48:43.230 回答
2

I had the same issue in Chrome and Firefox. I was able to alleviate the issue by having the server respond with the url of the page. For your case you could have the response be the new value for the user status. Here are two examples:

$.post('?action=' + myAction, function (data) {
        window.location.href = data;
    });

..and the server response

Response.Write("/yourUrl);
Response.End();

This eliminated the inconsistency in Chrome and the page reloads without fail.

于 2014-03-25T15:19:21.230 回答
2

看这个 您可以通过将 forceGet 参数设置为 true 来强制重新加载以从服务器获取页面:

location.reload(true);
于 2014-08-29T12:32:40.003 回答
1

在这里,这对我与 Fire Fox 以及 Crome 一起工作,并且与 IE 一起工作正常。

object.reload(forcedReload);
于 2017-01-12T05:50:35.590 回答
0

我尝试了不同的答案,但对我有用的答案是强制重新加载的论点“真”

setTimeout(()=>{
  window.location.reload(true);
});
于 2021-09-21T13:06:32.197 回答
0

我刚刚找到的超级解决方案。

您必须模拟发布一个空表单。

HTML

<form id="myForm" action='@Url.Action("Details","Main", new { id = @Model.ID } )'></form>

JS

document.getElementById("myForm").submit();
于 2019-04-02T06:40:10.107 回答
0

从我的角度来看,关键点是我的函数位置必须高于调用发起的位置。认为浏览器从上到下解释 javascript,如果我的重新加载代码低于调用它的对象,它就会失败。如果我把代码放在上面,window.location.href = window.location.href; 工作。

于 2016-09-09T18:18:03.500 回答