if i make a mistake in code and cause an infinite loop in javascript and it keeps on calling alert(), is there a way out to stop the loop?

StackOverflow https://stackoverflow.com/questions/894530

  •  23-08-2019
  •  | 
  •  

Question

sometimes i use debug code to alert something in javascript (for example, matching something in regular expression), but forget a modifier and and the alert is in an infinite loop (or if the loop matches the pattern 300 times). If using Firefox, the alert keeps on coming out, and there is no way to even close the tab, the window, or the app.

If I force exit, it will close all the tabs and even other windows of Firefox... is there actually a way to stop the loop more gracefully?

Was it helpful?

Solution

The short answer is: No.

This is one good reason to use Firebug and the console.log function. Which, ironically, will cause the "stop script because it's running away dialog" to not display in some cases, meaning you are right back where you are now.

Chrome and Opera have this feature. IE doesn't, Apple Safari doesn't either.

Not a native solution but you could try this grease-monkey script: http://www.tumuski.com/2008/05/javascript-alert-cancel-button/

Also, you could simply override the alert function to use a confirm dialog instead and stop showing alerts if the confirm is canceled:

var displayAlerts = true;

And then:

function alert(msg) {
  if (displayAlerts) {
     if (!confirm(msg)) {
       displayAlerts = false;           
     }
  }
}

OTHER TIPS

Looks like you can in firefox:

  1. Hold Ctrl
  2. Hold Enter
  3. Press F4 once

According to the blog post, it doesn't work in all cases, more information here: puremango.co.uk

Google Chrome allows you to prevent additional alerts from showing.

I've written a Firefox extension to combat this problem.

https://addons.mozilla.org/en-US/firefox/addon/13176

No way. Good thing is though most browsers have infinite recursion protection, but that's offtopic.

Install Greasemonkey and go to this page: http://www.tumuski.com/2008/05/javascript-alert-cancel-button/

After you install this script for greasemonkey, any alert dialog window will have a cancel button which it will stop the javascript.

[tested on IE 11] If you need to close the web browser and don't want to ctrl-alt-del for whatever reason (in my case my bad javascript infinite alert loop was running on citrix which would automatically reload my last session after a ctrl-alt-del from the cache memory which I can't clear)

you can bug your way out by holding down alt-F4 and spamming the X (to close the browser window).

This works really fast... The alt-F4 closes the alert boxes really fast and you will notice the X button at the top of your browser flickering. This gives you very small windows of time where you can close it between alerts.

Note: if you have multiple tabs open you need to close them from the icon on the control bar at the bottom of your screen first because the "close all tabs message" will interfere.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top