Javascript document.title 在 Safari 中不起作用

标签 javascript safari title

我使用的是 Safari 6.0.5。

我打开一个新的空窗口,尝试将标题更改为“调试窗口”,但没有任何反应。通过每 10 毫秒检查一次的检查函数,它表示 window.document.title 是“调试窗口”,但新窗口标题栏仍表示它是“无标题”。

var debug_window    =   window.open('', 'debug_window', 'height=200'); 

debug_window.document.title     =   'Debug Window';

    function check() 
    {

    debugLog(1, 'title:' + debug_window.document.title);
    if(debug_window.document) { // if loaded
        debug_window.document.title = "debug_window"; // set title
    } else { // if not loaded yet
        setTimeout(check, 10); // check in another 10ms
    }
     }
     check();

调试日志中的输出是:

17:35:04.558: title:
17:35:04.584: title:debug_window

这里出了什么问题,新窗口仍然被称为“无标题”?

谢谢!

最佳答案

现在 window.open() 的第二个参数是框架/窗口名称,也用作默认标题。这最终会被加载到该窗口中的文档覆盖。打开文档流并插入基本的 html 文档应该可以达到目的:

var debug_window  = window.open('', 'debug_window', 'height=200');
debug_window.document.open();
debug_window.document.write('<!DOCTYPE html>\n<html lang="en">\n<head>\n<title>Debug Window</title>\n</head>\n<body></body>\n</html>');
debug_window.document.close();

var debug_body = debug_window.document.getElementsByTagName('body')[0];
// write to debug_window
debug_body.innerHTML = '<p>Message</p>';

因此,您将在窗口内设置一个基本文档,就像服务器加载它一样(通过写入“文档流”)。然后您将开始像处理其他文档一样操作此文档。

<小时/>

编辑:在 Safari 中也不起作用。

其他建议:在服务器上设置一个基本文档(包括标题),并在加载时将内容注入(inject)到其正文中。作为奖励,您可以通过样式表设置 CSS。

var debug_window  = window.open('debug_template.html', 'debug_window', 'height=200');
debug_window.onload = function() {
  var debug_body = debug_window.document.getElementsByTagName('body')[0];
  debug_body.innerHTML = '...';
  // or
  // var el = document.createElement('p');
  // p.innerHTML = '...';
  // debug_body.appendChild(p);
  debug_window.onload=null; // clean up cross-reference
};

在服务器端类似

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Debug Window</title>
    <link rel="stylesheet" href="debug_styles.css" type="text/css" />
  </head>
  <body></body>
</html>

如果这仍然不起作用(例如:写入调试窗口的文档无效),您可以通过以下方式从调试窗口内部调用您的应用程序:

<body onload="if (window.opener && !window.opener.closed) window.opener.debugCallback(window, window.document);">
</body>

(因此,您将检查打开器(您的应用程序)是否存在并且同时尚未关闭,然后使用调试窗口及其文档作为参数在您的应用程序中调用回调函数“debugCallback()” .)

关于Javascript document.title 在 Safari 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17640862/

相关文章:

javascript - 在嵌套的 ng-repeat Angularjs 中重复父行?

javascript - 如何将表单数据传递给GAS

javascript - 如何使用 JavaScript 放大图像?

css - 在不重新加载页面的情况下重新加载 CSS 样式表?

ruby-on-rails - Ruby on Rails 中的自定义模型属性(列名)标题

ios - MKPlacemark 图钉标题

java - CometD 服务器抛出异常并且未一次推送所有消息

html - 复选框样式在 Safari 移动设备上不起作用

css - Flexbox 无法在 Safari 中工作,在 Web 检查器中被删除

bash - 从 bash 脚本更改 xterm 的标题