【问题标题】:On my webpage, using "window.location.href" returns "about:srcdoc" instead of the URL name在我的网页上,使用“window.location.href”返回“about:srcdoc”而不是 URL 名称
【发布时间】:2020-08-25 23:13:34
【问题描述】:

要查看问题,请输入此 URL“https://gainesrad.com/readqrcodefromkiosk/”

它应该显示网页的 URL,但我得到的是“about:srcdoc”。

这是 gainesrad.com/readqrcodefromkiosk 上的 HTML 代码

<!DOCTYPE html>
<html>
<body>
<b>This should tell me the URL of the page but instead is says "about:srcdoc"</b>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "The full URL is:  " + window.location.href;
</script>
</body>
</html>

【问题讨论】:

  • 在同一个域中尝试top.location.href

标签: javascript html url


【解决方案1】:

srcdocattribute 在 iframe 上下文中用于指示 iframe 的内容。 在检查页面的完整源代码时,我注意到您使用的是 iframe,这就是为什么您会收到 about:srcdocwindow.location.href

要获取 URL,请将内联 &lt;script&gt; 移动到 Iframe 之外的文档上下文中。

看到这个:https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement/srcdoc

由于您的 iframe 包含在 Document 上下文中,因此您可以执行此操作来检索包含文档的基本 URI: https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement/contentDocument

   let iframe = document.querySelector('iframe')
   console.log('The baseURI is: ' + iframe.contentDocument.baseURI)

【讨论】:

  • 感谢 Micah 的回复。听起来你有答案,但我遇到了麻烦,因为我不明白如何将内联
  • 这应该告诉我页面的 URL,而是说“about:srcdoc”

  • 你能帮我把内联
  • @BobGaines,在检查生成的 HTML 页面的 head 部分时,您正在使用将您的内容注入 iframe 的站点生成器,这就是为什么您不知道您正在使用 iframe .我已经更新了我的答案以包含一个解决方案。看看它是否适合你
【解决方案2】:

你们都是对的。我打电话给 godaddy,因为我使用的是网站生成器,所以我在 iFrame 中。 为了在 godaddy 上做我需要的事情,我不得不购买虚拟主机并停止使用 Website Builder。 感谢大家的帮助。

【讨论】:

    猜你喜欢
    • 2020-12-14
    • 1970-01-01
    • 2019-08-08
    • 2020-01-03
    • 2019-12-20
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 2017-10-13
    相关资源
    最近更新 更多