相关疑难解决方法(0)

如何监听title元素的更改?

在javascript中,是否有一种技术可以监听title元素的更改?

javascript dom-events

21
推荐指数
2
解决办法
9994
查看次数

通过Javascript检测文档标题的更改

有没有办法检测到document.title/ head > title通过Javascript 的更改?我想通过Google Chrome扩展程序内容脚本检测到这一点,因此我无法在目标页面的JS中实际连接执行标题更改的代码.

我发现WebKitMutationObserver理论上应该能够检测到更改head > title,但它并不适用于所有情况:

// set up an observer for the title element
var target = document.querySelector('title');
var observer = new WebKitMutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
        console.log(mutation);
    });
});
var config = { attributes: true, childList: true, characterData: true };
observer.observe(target, config);

// this jQuery statement fires the observer as expected ...
$('head > title').text('foo');

// ... but this doesn't:
document.querySelector('title').innerText = 'cheezburger';

// ... and neither does this:
document.title = …
Run Code Online (Sandbox Code Playgroud)

javascript jquery webkit google-chrome-extension

13
推荐指数
1
解决办法
7622
查看次数