【问题标题】:Can a webpage's title be accessed before the page is done loading?可以在页面加载完成之前访问网页的标题吗?
【发布时间】:2016-02-26 06:58:00
【问题描述】:

所以我正在构建一个 Chrome 插件,该插件需要访问用户可见页面的 DOM 以用于各种目的。我还需要使用页面的标题,但似乎没有必要等到整个页面通过以下方式加载:

$(document).on("DOMContentLoaded", function () {
    onLoad();
});

在页面完成加载之前,我是否可以访问页面的标题(因为它已经在浏览器中可见)?

我想这样做的原因是我的扩展程序发送(并等待响应)一个 API 调用(仅依赖于页面标题),所以我想尽快开始这个过程。

谢谢!

【问题讨论】:

    标签: javascript jquery html google-chrome web


    【解决方案1】:

    我还没有真正尝试过,但如果页面 header 在您的扩展脚本运行时已经加载,则以下应该为您提供标题:

    document.head.getElementsByTagName('title')[0].innerText
    // EDIT: Or I guess you could just use document.title
    

    某些网站可能会使用脚本修改页面标题,因此最好在页面完全加载后验证标题。

    【讨论】:

      【解决方案2】:

      尝试将包含document.title="title"<script> 元素放在<title> 元素之前。这应该在加载body 之前设置documenttitle

      document.body.textContent = document.title
      <!doctype html>
      <html>
      <head>
      <script>
        document.title = "def"
      </script>
        <title>abc</title>
      </head>
      <body>
      </body>
      </html>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多