Javascript 在重新加载期间通过 url 发送参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10665235/
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-24 02:25:18  来源:igfitidea点击:

Sending parameters via url during reload

javascripthtmlurlwindow.location

提问by ChrisOdney

I am doing an ajax request and in the success callback I want to refresh the page to reflect the changes. I am trying to pass parameters to the same page as I refresh that page.

我正在执行 ajax 请求,并且在成功回调中我想刷新页面以反映更改。我试图在刷新该页面时将参数传递到同一页面。

The url is something like:

网址是这样的:

http://localhost:8080/details#component/12to which I am appending the parameter as said below.

http://localhost:8080/details#component/12我将如下所述附加参数。

window.location.href += "?code=approved";
window.location.reload();

The above works in Firefox but not in IE, can you please help here?

以上在 Firefox 中有效,但在 IE 中无效,你能在这里帮忙吗?

Chris.

克里斯。

回答by Coder

Try using these for IE:

尝试将这些用于 IE:

window.location.reload(false); // To get page from cache
window.location.reload(true); // To get page from server

回答by Willem Joosten

The hash is the problem; you append your data to the URL fragment (part after the #) The fragment isn't send to the server, ie the url doesn't change so no need to request the page again. Try manually adding '#some_text' to the url in your browser and see what happens ;)

哈希是问题所在;您将数据附加到 URL 片段(# 之后的部分)该片段不会发送到服务器,即 url 不会更改,因此无需再次请求页面。尝试在浏览器的 url 中手动添加“#some_text”,看看会发生什么;)

Try something like this:

尝试这样的事情:

var newloc = document.location.href;
if(document.location.hash){
    // remove fragment
    newloc = newloc.substr(0, newloc.indexOf(document.location.hash));
}
newloc += document.location.search ? ";" : "?"; // prevent double question mark
newloc += 'code=approved';
// append fragment back to url

回答by Eyal Ch

if window.location.hashis empty, you cant assign to location.href a new value without using a correct function (at least tested in chrome).

如果window.location.hash为空,则不能在不使用正确函数的情况下为 location.href 分配新值(至少在 chrome 中测试过)。

try the window.location.replace:

试试window.location.replace

if (!window.location.hash) 
    {
        window.location.replace(window.location.href + "?code=approved")
    }