JavaScript to reload the page as GET request

早过忘川 提交于 2020-04-06 02:03:56

问题


I have a seemingly simple question, but can't find the answer. I have a webpage, which may have resulted from a POST request and may have an anchor (#) in the URL. I want to reload this page as a GET request in JavaScript. So it's similar to this question, but I actually want to avoid the POST, not just the warning about it.

So, for example, if the page resulted from a POST request to "http://server/do/some?thing#" I want to reload the URL "http://server/do/some?thing" as a GET. If I try

window.location.reload(true);

that causes IE to try a POST. If I instead do:

window.location = window.location.href;

this does nothing when the URL has an anchor. Do I really need to do string manipulation myself to get rid of the "#whatever" or is there an easier, "better" way to do this?


回答1:


The best I've come up with so far is:

function reloadAsGet()
{
    var loc = window.location;
    window.location = loc.protocol + '//' + loc.host + loc.pathname + loc.search;
}



回答2:


Try the following:

location.replace(location.href)



回答3:


You can try this

location=location.href


来源:https://stackoverflow.com/questions/1225337/javascript-to-reload-the-page-as-get-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!