For a userscript I need to clear a page including all of its styles, contents, events and timers. Is it possible to do that without redirecting to about:blank
?
I tried document.body.remove(); document.head.remove();
but timers weren't disable.
How can I clear the HTML page properly?
to remove timers you can request an empty setTimeout and then try to decrease its id and call clearTimeout for each id. You can do same about setInterval.
*Edit: As bikeshedder said, The type of the timeoutID is not defined in the MDN. It could be anything from a random number to an UUID. The MSDN defines it as an Integer but does not say that IDs need to grow bigger over time. It is quite possible that an ID of a stopped timeout/interval is reused. Relying on this is not portable and might break in the future.
But at same time, above way is only possible way you can go to clear all timers. Al least it still works AFAIK.
Personally I suggest to keep ID of all setTimeout and setInterval if you can.