views:

88

answers:

4

For some reason, using window.location.href doesn't change the URL in the user's address bar. Is there any reason why I'm getting this behavior?

CODE

Earlier, I posted to code here. But I see that I'm in a frame. For anyone who happens to have the same issue, window.top.location.href = 'page.htm'; will do the trick.

PS. Apologies for not mentioning the frame aspect. It was an tiny, subtle use of frames. Had I known, I wouldn't have asked the question :)

Thanks to all!

+1  A: 

Huh? Setting the window.location.href property will take the user to the given URL.

For example:

window.location.href = "http://google.com"

Will take the user to google.com -- the change will be visible in the address bar as well.

I can't believe I have to do this, but here's a jsfiddle test...

CD Sanchez
not so :) not happening for me at least
Emile
@Emile: Which browser are you using?
CD Sanchez
@Daniel, I've tried on Firefox 3.6 and IE 8
Emile
A: 

You can't programatically change the address bar (think of the phishing possibilities).

Best you can do is change the url with window.location, i.e. navigate the user there.

The process of the address bar changing this way is abstracted away :)

alex
I could see your point about phishing possibilities, but that wouldn't explain why there isn't a mechanism to update the address bar with an URL of the page that the user is actually visiting. However, you're probably right, bearer of bad news :) If someone doesn't have a solution, I'll accept this.
Emile
@Emile: If you aren't more specific nobody is going to be able to help you. There is a mechanism - it's changing the `href` property of `window.location`.
CD Sanchez
@Daniel, I've been very responsive to your clarification questions.
Emile
@Emile, you've been very responsive, but what you are describing makes no sense. Perhaps your browsers are infected? Have you tried on a different machine? Have you created a simple test page as per Daniel's example? Have you tried Daniel's example?
Kirk Woll
@Kirk, I did try those examples and admitted that the expected behavior wasn't the behavior I was getting. I also posted all my code. I finally found out that I was in a frame and updated by answer to reflect that. Thanks for considering some possibilities!
Emile
@Emile, thanks for the answer. In hindsight, that should have been a guess of ours. :) Very glad you found a solution!
Kirk Woll
+1  A: 

You can do it with a frameset and the adress bar won't change, no matter where users navigate to.

But as already mentioned, even Internet Explorer -since IE7- focuses on the User to prevent stuff like that, the User has the right to know where he is surfing to - it is a security issue.

Imagine you come to some website that looks clean and friendly and the Site redirects you to an array of phishing sites without you or your browser security noticing it. The Site owner could get all your private info, for e.g. your clipboard content or geolocation data and while you are at ease, the Site owner empties your bank account. Just an example.

In addition to your below answer I tried window.location.href on Firefox 3.6 and it works as expected.

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>urlRefresh</title>


</head>
<body>

<input type="button" value="changeAdress" id="changeAdress" />

<script>

document.getElementById('getValues').onclick = function() { 
    window.location.href = "http://www.bing.com";
}
</script>

</body>
</html>


If you click the button changeAdress JavaScript issues a GET Request via your browser to the desired Website.

See http://plixi.com/p/46770650

Stephan Kristyn
"the User has the right to know where he is surfing to" True. So it would seem that the URL should refresh to reflect the window.location changes.
Emile
It should absolutely. I edited my answer to show you how.
Stephan Kristyn
Thanks for the demonstration! You are right that I should be getting a different address in the address bar.
Emile
A: 

window.top.location.href = 'home.html' changed the address bar for me, because unknowingly I was caught in a frame.

Thanks Stack Overflow for at least confirming that the behavior I was getting was unusual.

Emile