Javascript 为什么 window.location 不会加载新页面?

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

Why won't window.location load a new page?

javascripthtmlurlredirectwindow.location

提问by IronicMuffin

This bit of code used to work, and now it doesn't:

这段代码曾经可以工作,现在不行:

var url = myurl +'?id=' + id + '&phase=' + phase;
window.location = url;

Using the IE dev toolbar I've verified that url has a valid url, and window.location returns the new url...the only problem is the page does not reload.

使用 IE 开发工具栏我已经验证 url 具有有效的 url,并且 window.location 返回新的 url...唯一的问题是页面不会重新加载。

Does anyone know of any reasons for window.location to now actually load a new document when it is assigned to?

有谁知道 window.location 现在在分配给新文档时实际加载新文档的任何原因?

采纳答案by IronicMuffin

Thanks to @Senad Meskin.

感谢@Senad Meskin

document.location = url;

worked for me. Any reason why window would stop working?

为我工作。窗口会停止工作的任何原因?

回答by Nicholas Welch

Use window.location.href = url;instead.

使用window.location.href = url;来代替。

回答by Manolo

Is your JavaScript running the onclick event from an <input> type="button" or type="submit"?

您的 JavaScript 是从 <input> type=" button" 还是 type=" submit"运行 onclick 事件?

If your button is type="submit" it won't work unless you stop the submission from executing.

如果您的按钮是 type="submit" 它不会工作,除非您停止执行提交。

I know this is kind of obvious but I didn't see this in the other comments so I might just add to this thread.

我知道这很明显,但我没有在其他评论中看到这一点,所以我可能会添加到这个线程中。

回答by William

So i just got this same error today, I for some reason couldn't get my page to redirect to google (just for testing purposes). Copied the code directly from another site and it worked, so i tried google.com again - low and behold it didn't work...

所以我今天遇到了同样的错误,由于某种原因,我无法让我的页面重定向到谷歌(仅用于测试目的)。直接从另一个站点复制代码并且它起作用了,所以我再次尝试了 google.com - 低,但它没有起作用......

I should also mention that i'm trying to redirect a page in an iframe, not the entire webpage.

我还应该提到我正在尝试在 iframe 中重定向页面,而不是整个网页。

So I fired up my trusty Max's HTML Beauty++ 2004 and opened my page in that, sure enough it gave me my error:

所以我启动了我可信赖的 Max 的 HTML Beauty++ 2004 并在其中打开了我的页面,果然它给了我我的错误:

This content cannot be displayed in a frame

 To help protect the security of information you enter into this  
 website, the publisher of this content does not allow it to be
 displayed in a frame.

此内容无法在框架中显示

 To help protect the security of information you enter into this  
 website, the publisher of this content does not allow it to be
 displayed in a frame.

The code i Used to get that:

我用来得到的代码:

window.location = "http://www.google.com";  

It looks like browsers don't display this error, instead they just don't redirect you... Kind of confusing :/

看起来浏览器不会显示此错误,而是不会重定向您......有点令人困惑:/

回答by Sudipta Chatterjee

I was using jquery, and a similar problem cropped up and I had to resolve it by adding the data-ajax=falseattribute to the <a href="...link.

我正在使用 jquery,并且出现了类似的问题,我不得不通过将data-ajax=false属性添加到<a href="...链接来解决它。