【问题标题】:Display iFrame url in address bar在地址栏中显示 iFrame url
【发布时间】:2014-05-09 08:17:29
【问题描述】:

是否可以在 url 地址栏中包含 iframe 内容的 url?

例如,我有一个域 sub.test.com,它有一个 iframe,src 指向 realpage.com

浏览页面逻辑上不会改变地址栏中的任何内容,因为我们是在其他域的 iframe 中打开 realpage。

我知道无法将整个 url 注入地址栏,但是没有主机名怎么办?

例如,如果我在iframe 中打开站点realpage.com/first.php,我希望地址栏中的网址更改为sub.test.com/first.phpsub.test.com/first

【问题讨论】:

标签: javascript html url iframe address-bar


【解决方案1】:

这里有两个选项,您可以使用pushState (more info) 或使用哈希键导航 (more info),如果您希望使其与尚不支持 pushState 的浏览器兼容.

pushState 示例:

var stateObj = { foo: "bar" };
history.pushState(stateObj, "page 2", "bar.html");

哈希键示例:

if ("onhashchange" in window) {  
  alert("The browser supports the hashchange event!");  
}  

function locationHashChanged() {  
  if (location.hash === "#somecoolfeature") {  
    somecoolfeature();  
  }  
}  

window.onhashchange = locationHashChanged;

更多信息在这里是另一个链接:Change the URL in the browser without loading the new page using JavaScript

【讨论】:

    【解决方案2】:

    顶级框架可以使用the history api 更改地址(尽管不是原始部分),具体参见pushState

    要响应框架中的导航来执行此操作,因为它是跨域的,您需要使用 postMessage 发送您想要更改的 URL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-01
      • 2015-04-23
      • 2016-07-28
      • 1970-01-01
      • 2023-03-28
      • 2016-05-13
      • 2014-04-04
      • 2013-01-12
      相关资源
      最近更新 更多