jQuery window.location.reload 不适用于 Firefox 和 Chrome

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18967532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 22:52:36  来源:igfitidea点击:

window.location.reload not working for Firefox and Chrome

javascriptjquerygoogle-chromefirefoxreload

提问by UID

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

但在这种情况下,后端的状态会发生变化,但要显示页面需要刷新的状态,因为在刷新时它会检查当前状态并显示。所以我使用“window.location.reload”属性来显示页面上的最新状态

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.

所有的东西在 IE 中都运行良好。但在 Firefox 和 Chrome 的情况下,状态不会改变。我认为“window.location.reload”不起作用,因为当我只是评论这一行并尝试单击按钮并手动刷新页面时,它会显示更改状态。

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

你能建议我应该用什么来在 Firefox 和 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.

当我用谷歌搜索时,如果你在“setTimeout()”中给出它,我发现它可以在 Firefox 和 Chrome 中工作。我试过了,但即便如此,它也不适合我。

<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!

请建议!

回答by Wellington Zanelli

I have two other options to you:

我还有另外两个选择:

history.go(0);

And:

和:

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

I'm not tested it on Firefox and Chrome yet, but it may help you faster. Tomorrow I'll do the tests and update the answer if this not work.

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

回答by Morteza Ziyae

I had this problem in AngularJS and in Firefox. (Reloading was fine in Chrome). By using setTimeout problem solved.

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

setTimeout(function(){
  window.location.reload();
});

回答by Shylo Hana

Have you tried this?:

你试过这个吗?:

window.location = window.location;

Apparently you can drop the "window.", but haven't tried it myself.

显然您可以删除“窗口”,但我自己还没有尝试过。

回答by Michael

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:

我在 Chrome 和 Firefox 中遇到了同样的问题。我能够通过让服务器响应页面的 url 来缓解这个问题。对于您的情况,您可以将响应作为用户状态的新值。这里有两个例子:

$.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.

这消除了 Chrome 中的不一致,页面重新加载没有失败。

回答by Volodymyr Yasinskyi

Look thisYou can force the the reload to get the page from server by setting the forceGet parameter to true:

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

location.reload(true);

回答by Dhruv Gurjar

Here this is working to me with Fire Fox as well as Crome and its working fine with IE.

在这里,这对我来说适用于 Fire Fox 和 Crome,并且它与 IE 一起工作得很好。

object.reload(forcedReload);

回答by Developer

Mega solution I just have found.

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

You have to simulate to post an empty form.

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

HTML

HTML

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

JS

JS

document.getElementById("myForm").submit();

回答by Matt Meents

Key note from my perspective is that the location of the function for me had to be above the location in which the call originated. Thinking that browser interprets the javascript from top to bottom, if my code for reload was below the object that invokes it, it fails. If I put the code above it, window.location.href = window.location.href; worked.

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