使用iframe时更改地址栏 [英] Changing address bar when using iframe

查看:220
本文介绍了使用iframe时更改地址栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有办法使用iframe,但在点击iframe内部的内容时,仍然会更改地址栏?我希望页面顶部有一个水平条,带有一个音乐播放器,可以在浏览网站时播放音乐。但我也希望人们能够轻松地为页面添加书签。

Is there a way to use an iframe, but still have the address bar change when clicking what's inside the iframe to what the page actually is? I want to have a horizontal bar at the top of the page with a music player that allows music to play while browsing the site. But I also want people to easily be able to bookmark the pages as well.

我已经搜索过,但找不到答案。许多人只是想改变iframe中加载的URL ...这不是我想要的。我希望浏览器中显示的实际地址栏能够更新。

I've searched, but can't find the answer. Many are just looking to change what URL is loaded in the iframe...that's not what I want. I want the actual address bar displayed in the browser to update.

推荐答案

这句话是为了帮助我达到30个字符的最小值。忽略这一点并阅读下一句的答案:

This sentence is here to help me reach the 30 character minimum. Ignore this and read the next sentence for your answer:

否。

更新:

更改和重定向是两回事。你需要澄清它是什么。 更改地址栏是不可能的。
重定向是可能的。

"Changing" and "Redirecting" are two different things. You need to clarify which it is. "Changing" the address bar is impossible. "Redirecting" IS possible.

点击iframe中的标准链接不会影响最顶层的窗口。 iframe中的Javascript重定向的快速示例如下所示:

Clicking on a standard link from within an iframe will not affect the topmost window on its own. A quick example of a Javascript redirect within an iframe would be something like this:

TOP WINDOW:

<html>
<head></head>
<body>
    <iframe src="http://yoursite.com/iframe_test.html" />
</body>

IFRAME TEST:

<html>
<head>
    <script>
    window.onload = ready;
    function ready()
    {
        document.getElementById("link1").onclick = linkClick;
        document.getElementById("link2").onclick = linkClick;
    }
    function linkClick(e)
    {
        e.preventDefault();
        window.top.location.href = this.href;
    }
    </script>
</head>
<body>
    <a id="link1" href="http://youtube.com">Link1</a>
    <a id="link2" href="http://facebook.com">Link2</a>
</body>

说明:

如果没有使用Javascript,当您点击iframe中的链接时会发生的情况是您将保留在原始页面上,并且iframe将重定向到您点击的链接。以这种方式在iframe中使用Javascript可以强制最顶层的帧重定向。

Without the Javascript in place, what will happen when you click on a link in an iframe is that you will remain on the original page and the iframe will be redirected to the link you've clicked on. Using Javascript within an iframe in this fashion can force the topmost frame to redirect.

我强烈反对以任何方式依赖于此。一般来说这是一个草率的编程,如果可能的话,我会完全远离iframe。

I would highly discourage relying on this in any way. It's sloppy programming in general, and I would stay away from iframes altogether if possible.

这篇关于使用iframe时更改地址栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆