最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - window.location.href but where the address bar changes - Stack Overflow

programmeradmin0浏览0评论

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!

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!

Share Improve this question edited Sep 24, 2010 at 2:53 Kyle Cureau asked Sep 24, 2010 at 1:34 Kyle CureauKyle Cureau 19.4k23 gold badges77 silver badges104 bronze badges 6
  • 3 Can you provide a code sample? Because window.location.href normally does what I think you want to do… So either I don't understand what you want or something else is wrong. – David Wolever Commented Sep 24, 2010 at 1:56
  • in a callback, I've added window.location.href = "home.html"; So the page call and redirect are made, but the url remains the same (IE and FF tested) – Kyle Cureau Commented Sep 24, 2010 at 2:05
  • @Emile: Have you confirmed that the callback is being called? You should provide code. – Cristian Sanchez Commented Sep 24, 2010 at 2:08
  • @Daniel, Yes. I've also just tested changing the code to an external URL window.location.href = "google." and no luck. But now I believe you guys that the address bar should change. I tested it in clean code space and it does what you all say it should. Any other ideas? – Kyle Cureau Commented Sep 24, 2010 at 2:10
  • @Emile: Could you post the code, including where you assign the callback and when it's triggered? – Cristian Sanchez Commented Sep 24, 2010 at 2:11
 |  Show 1 more ment

3 Answers 3

Reset to default 4

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.

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 e 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.";
}
</script>

</body>
</html>


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

See http://plixi./p/46770650

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

发布评论

评论列表(0)

  1. 暂无评论