In some browsers, this will not happen in the first place.
Whenever this happens to me, I do my best to quickly execute the following key sequence:
[ENTER] Ctrl+W
Which exits the loop and closes the window / tab, but should not interrupt the browser.
EDIT , . , javascript.