问题
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!
回答1:
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.
回答2:
I had this problem in AngularJS and in Firefox. (Reloading was fine in Chrome). By using setTimeout problem solved.
setTimeout(function(){
window.location.reload();
});
回答3:
Have you tried this?:
window.location = window.location;
Apparently you can drop the "window.", but haven't tried it myself.
回答4:
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.
回答5:
Look this You can force the the reload to get the page from server by setting the forceGet parameter to true:
location.reload(true);
回答6:
Here this is working to me with Fire Fox as well as Crome and its working fine with IE.
object.reload(forcedReload);
回答7:
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.
回答8:
Mega solution I just have found.
You have to simulate to post an empty form.
HTML
<form id="myForm" action='@Url.Action("Details","Main", new { id = @Model.ID } )'></form>
JS
document.getElementById("myForm").submit();
来源:https://stackoverflow.com/questions/18967532/window-location-reload-not-working-for-firefox-and-chrome