Kyle Cureau
Kyle Cureau

Reputation: 19366

window.location.href but where the address bar changes

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!

Upvotes: 0

Views: 5726

Answers (3)

Kyle Cureau
Kyle Cureau

Reputation: 19366

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.

Upvotes: 4

Stephan K.
Stephan K.

Reputation: 15702

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

Upvotes: 1

alex
alex

Reputation: 490163

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

Upvotes: 0

Related Questions