javascript - How to prevent any iframe or any other script to change title of document in the top window? -
normally, delete , re-define title property following code.
if (delete document.title) { object.defineproperty(document, 'title', { get: getter, set: setter, enumerable: true, configurable: false }); }
however, found page still can use following code change title.
$(window.top.document.head).find('title').text('new title')
is there way prevent it?
thanks,
hook observer title element. modified mdn's example below:
// select target node var target = document.getelementsbytagname("title"); console.log(target[0]); // create observer instance var observer = new mutationobserver(function(mutations) { mutations.foreach(function(mutation) { alert("title changed!"); // treat mutation here. use mutation.type see happened. }); }); // configuration of observer: var config = { attributes: true, childlist: true, characterdata: true }; // pass in target node, observer options observer.observe(target[0], config); function changetitle(){ document.title = "something else"; }
<!doctype html> <html><head><meta http-equiv="content-type" content="text/html; charset=windows-1252"> <title>a test</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script src="test.js"></script> </head> <body> <a href="" onclick="changetitle();">change title</a> </body></html>
Comments
Post a Comment