Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making document.title untouchable for Javascript

Is it possible to make document.title (<head><title> .. </title></head>) impossible to change for Javascript ?

My problem is that iny my project there are some javascripts that change document.title every 1 second and I want title to stay the same. Unfortuantely I am not able to change or remove those JS files. I've tried a to think of workaround, something like that :

function checkTitle() {
if(document.title != oldTitle)
document.title = oldTitle;
}
setInterval(checkTitle(), 100);

It seems like a good idea, BUT unfortuantely there I have also counter which displays timer on my site. My whole code looks like this :

var ticks = 0;

function update() {
    if(ticks % 1000 == 0) {
    ...update timer and some other stuff...
    }
    ticks+=100;
    if(document.title != oldTitle)
    document.title = oldTitle;
}
setInterval(update, 100);

Such code after +- 100 seconds is beggining to slow down and my timer definitely is not updated every 1 second.

So the question is : Is it possible to make html element (..) impossible to change for javascript, if not, is there any workaround for my problem ?

Thanks in advance.

like image 763
pzeszko Avatar asked Jan 08 '15 19:01

pzeszko


People also ask

How do you display a title in Javascript?

Put in the URL bar and then click enter: javascript:alert(document. title); You can select and copy the text from the alert depending on the website and the web browser you are using.

How do you create a title in Javascript?

The title element can be selected by specifying the title element in the selector as a parameter. This will return the current title element of the page. The textContent property of an element returns the text content of a specific node.

What does title do in Javascript?

The title attribute specifies extra information about an element. It can be shown as a tooltip text when the mouse moves over the element.

How do you create a dynamic title in HTML?

If you are loading via ajax and you want to dynamically change the page title with just Javascript, then do: document. title = 'Put the new title here'; However, search engines will not see this change made in javascript.


1 Answers

This probably has some side effects, but I'm not sure what exactly, but you can redefine the title property and make it non-writable, which would be better than sealing or freezing the entire document as it only affects the title

Object.defineProperty(document, 'title', {
  enumerable: false,
  configurable: false,
  writable: false,
  value: document.title
});

FIDDLE

This should work in all browser from IE9 and up, and maybe IE8, which I haven't tested, but I think only DOM nodes can be changed in IE8, and not sure if document counts ?

like image 176
adeneo Avatar answered Sep 19 '22 15:09

adeneo