开发者

Refresh page using javascript not working

开发者 https://www.devze.com 2023-04-11 02:31 出处:网络
I send a post request to the server, which inturn returns a JSP. Now if i use the refresh button of the browser, i get a confirmation to resubmit the data, and then the page reloads successfully.

I send a post request to the server, which inturn returns a JSP. Now if i use the refresh button of the browser, i get a confirmation to resubmit the data, and then the page reloads successfully.

However, when i try to refresh the page using javascript, i dont get any confirmation box, and the reload also doesnt work. Ive tried all methods of reload like :

  • location.reload()
  • window.location.reload()
  • window.location.href = window.location.href
  • history.go(0);

I think the problem is that its sending a get request when i try to r开发者_运维百科efresh using javascript, and it is not able to find the necessary paramaters.

This problems occurs only in chrome... it works in Firefox and IE!!


The reload method may use the cache to reload the page. Specify the force parameter to make it get the page from the server:

window.location.reload(true);


parent.window.location.reload(true);

worked on chrome for me ... finally

explanation: of parent.window

A reference to the parent of the current window or subframe.

If a window does not have a parent, its parent property is a reference to itself.

When a window is loaded in an , , or , its parent is the window with the element embedding the window.


The only solution is to call the same URL with some additional value.

This is regular reloading script, this works fine on IE & FF but not on Chrome & Safari.

window.location = location.href; 

To make reload work on all browser types use:

window.location = location.href + '?session=' + sMath.floor((Math.random() * 1000000) + 1);

Tested on IE, FF, Chrome and Safari.


I tried all the possible solutions found (ajax calls, various snippets) but the only thing worked for me was disabling the network cache from chrome see here Why isn't Google Chrome reloading my scripts? I don't think it is a solution for production but while developing can spare you some time from searching. The problem seems to be with chrome's cache and even setting

window.location.reload(true);

does not clear the cache while reloading. On FF works fine.


In order to "refresh" the page via a post request, you need to submit a form. You can use any form already on the page, or create one programmatically.

var myForm = document.createElement("form");
myForm.action = "/my/url.jsp"; // Or use window.location, or something else.
myForm.method = "POST";
document.body.appendChild(myForm); // Needs to be in the DOM. Is not visible.
myForm.submit();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号