I'm working on a chrome extension, and I set window.title in the onload handler. It seems, though, that the page I'm modifying sets the document title dynamically as well. There's a huge collection of scripts being linked. Is there any way for me to prevent anyone else from modifying document.title
or any of its variants, without knowing where the modification is coming from? Alternatively, is there a quick way for me to see where the change is coming from?
Asked
Active
Viewed 1,344 times
4

Moshe
- 57,511
- 78
- 272
- 425
-
1**[This](http://stackoverflow.com/questions/2497200/how-to-listen-for-changes-to-the-title-element)** might be useful to you. Haven't tried the code in that post, but if it works, then you could set a callback that reset the title back to whatever you want, whenever some script changes it. – Christofer Eliasson Aug 13 '13 at 16:54
-
Good call, let me try it. – Moshe Aug 13 '13 at 16:57
-
Seems to work, want to post that as an answer? – Moshe Aug 13 '13 at 17:04
-
Glad it worked, posted it as an answer as well now. Sorry about the delay. – Christofer Eliasson Aug 14 '13 at 06:20
3 Answers
5
I had same problem, some external scripts are changed my page title by document.title = "..."
I've made own solution for it:
try {
window.originalTitle = document.title; // save for future
Object.defineProperty(document, 'title', {
get: function() {return originalTitle},
set: function() {}
});
} catch (e) {}

mixalbl4
- 3,507
- 1
- 30
- 44
1
See the answer to how to listen for changes to the title element?. Notably:
function titleModified() {
window.alert("Title modifed");
}
window.onload = function() {
var titleEl = document.getElementsByTagName("title")[0];
var docEl = document.documentElement;
if (docEl && docEl.addEventListener) {
docEl.addEventListener("DOMSubtreeModified", function(evt) {
var t = evt.target;
if (t === titleEl || (t.parentNode && t.parentNode === titleEl)) {
titleModified();
}
}, false);
} else {
document.onpropertychange = function() {
if (window.event.propertyName == "title") {
titleModified();
}
};
}
};
0
This SO answer suggest a technique for how to listen for changes to the document title.
Perhaps you could use that technique to create a callback which changes the title back to whatever you want it to be, as soon as some other script tries to change it.

Community
- 1
- 1

Christofer Eliasson
- 32,939
- 7
- 74
- 103