Reputation: 51
If a specific <div id>
is removed from the HTML document, I want it the user to be redirected to a new web page using JavaScript.
For example:
<div id="credits"></div>
If someone removes it then users will be automatically redirected to my website.
This is to protect copyrights.
Upvotes: 2
Views: 444
Reputation: 154878
You want a MutationObserver, but it's not widely supported: http://jsfiddle.net/xNAXd/.
var elem = document.getElementById("credits");
new MutationObserver(function(mutations) {
for(var i = 0; i < mutations.length; i++) {
var index = Array.prototype.indexOf.call(mutations[i].removedNodes, elem);
if(~index) {
alert("Deleted!");
break;
}
}
}).observe(elem.parentNode, {
childList: true
});
Upvotes: 0
Reputation: 83356
The best you can probably do is to just poll for the existence of that div, and redirect if it's not there. Also, be sure to check that it's actually visible, per Philip's comment.
But of course any user can just turn this script off, so I'm really not sure it's even worth the effort.
setInterval(function(){
if (!$('#credits:visible').length) window.location.href = 'wherever.com';
}, 3000);
Upvotes: 3