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

5

5

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?

Lanneret answered 20/7, 2011 at 15:15 Comment(8)
Yeah, try printing out the URL manually and see what it actually contains.Incoordination
For my problem, it is another url in my system.Lanneret
Doesn't work with 'www.google.com' eitherLanneret
try document.location and you will see resultsStaciastacie
Are you sure that the code is actually executing? Have it pop up an alert just before then or something, and make sure there are no errors tooElaterium
FYI document.location is sometimes readonly and isn't necessarily cross-browser since it's not part of the DOM spec. developer.mozilla.org/en/document.locationMonarski
What version of IE are you running? This should work..Unfasten
I'm running IE8, and it's a corporate app, so that's all I need to worry about. The code is definitely executing, I stepped through it with the VS Debugger.Lanneret
L
-1

Thanks to @Senad Meskin.

document.location = url;

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

Lanneret answered 20/7, 2011 at 15:19 Comment(1)
No, but document.location is not cross-browser either, since originally it was read-only. You probably won't hit any issues, but you may want to research more on why it isn't running.Monarski
D
13

Use window.location.href = url; instead.

Dripstone answered 20/7, 2011 at 15:17 Comment(2)
No javascript errors. This was working for me before, and I'm not sure what changed to cause it to stop.Lanneret
"This was working for me before, and I'm not sure what changed..." - figure out what changed. It was NOT this code, I'm guessing.Stenopetalous
C
4

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.

Caryopsis answered 1/11, 2013 at 14:17 Comment(0)
P
1

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

Pippy answered 5/5, 2012 at 22:33 Comment(0)
O
0

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.

Ofay answered 7/7, 2013 at 23:59 Comment(0)
L
-1

Thanks to @Senad Meskin.

document.location = url;

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

Lanneret answered 20/7, 2011 at 15:19 Comment(1)
No, but document.location is not cross-browser either, since originally it was read-only. You probably won't hit any issues, but you may want to research more on why it isn't running.Monarski

© 2022 - 2024 — McMap. All rights reserved.