views:

873

answers:

6

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?

+15  A: 

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;           
     }
  }
}
altCognito
Yes, Opera has a "Stop executing scripts in this page" checkbox at the bottom of alert windows
Perspx
+2  A: 

Google Chrome allows you to prevent additional alerts from showing.

Shane Fulmer
You'd think this would have been an obvious feature for all browsers.
ceejayoz
It always suprised me that browser didn't have this. Thank god chrome introduced it.
Pim Jager
A: 

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

Dmitri Farkov
+1  A: 

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

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

Mike
A: 

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.

A: 

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

Alex Key
btw. I'd recommend using console.log, but if you have got yourself in an alert() loop (like I just have, it's a good escape).
Alex Key