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

廉价感情. 提交于 2019-12-07 04:48:24

问题


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.

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


回答1:


Use window.location.href = url; instead.




回答2:


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

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

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.




回答3:


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...

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

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

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.

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 :/




回答4:


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




回答5:


Thanks to @Senad Meskin.

document.location = url;

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



来源:https://stackoverflow.com/questions/6764243/why-wont-window-location-load-a-new-page

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