UID
UID

Reputation: 4514

window.location.reload not working for Firefox and Chrome

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!

Upvotes: 26

Views: 132016

Answers (10)

mark delaforce
mark delaforce

Reputation: 1

window.location.href = window.location.href; didn't work for me. However, putting window.location.href into a variable first did so it's another option you can try.

let url = window.location.href;
window.location.href = url;

Upvotes: 0

Romain BOBOE
Romain BOBOE

Reputation: 377

I try different answer but the one that works for me is with the argument 'true' that forces the reload

setTimeout(()=>{
  window.location.reload(true);
});

Upvotes: 0

NoWar
NoWar

Reputation: 37633

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();

Upvotes: 0

Wellington Zanelli
Wellington Zanelli

Reputation: 1964

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.

Upvotes: 44

Dhruv Gurjar
Dhruv Gurjar

Reputation: 115

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

object.reload(forcedReload);

Upvotes: 1

Morteza Ziyaeimehr
Morteza Ziyaeimehr

Reputation: 2117

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

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

Upvotes: 41

Mattei
Mattei

Reputation: 1

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.

Upvotes: 0

Volodymyr Yasinskyi
Volodymyr Yasinskyi

Reputation: 148

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

location.reload(true);

Upvotes: 1

Michael
Michael

Reputation: 339

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.

Upvotes: 2

Shylo Hana
Shylo Hana

Reputation: 1860

Have you tried this?:

window.location = window.location;

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

Upvotes: 12

Related Questions