Manually abort JavaScript?

C

Chuck Anderson

Okay, ... so I'm playing around with forms and wrote a JavaScript
function that loops through all form elements and displays every option
in every select field with an alert for each one.

var els = form.elements;
var l = els.length;
for (var i=0; i<l; i++) // loop through all form elements
{
if (els.type.substring(0,6) == 'select')
{
var options = els.options;
for (var j=0, len=options.length; j<len; j++)
{
alert(els.name + ' option ' + j + ' = "' + options[j].text + '"')
}
}
}

In the form I am using this results in many alert boxes popping up.

This seems like a very, very basic question, but I've Googled it quite a
bit and can not find an answer. Is there a way to manually abort that
script (other than closing my browser) or do I have to keep clicking OK
until the loop has run it's complete course?
 
L

Lasse Reichstein Nielsen

Chuck Anderson said:
This seems like a very, very basic question, but I've Googled it quite
a bit and can not find an answer. Is there a way to manually abort
that script (other than closing my browser) or do I have to keep
clicking OK until the loop has run it's complete course?

That depends on the browser.

Opera version 9 has a checkbox marked "Stop executing scripts on this page"
in the alert window, which stops your code pretty effectively.
Other browsers might have their own versions of this, although I would
not expect IE to have actual usefull features :)

/L
 
K

Kevin Darling

Chuck said:
This seems like a very, very basic question, but I've Googled it quite a
bit and can not find an answer. Is there a way to manually abort that
script (other than closing my browser) or do I have to keep clicking OK
until the loop has run it's complete course?

You could use a custom prompt instead of alert, and allow yourself a
way out.

The majority of us tend to just hold down the Enter key :) The
problem with that is, you often trigger the original looping event
again! So here's a trick I use when I have large loops...

Just after the "for (;;;)" loop, put " document.body.focus()". That
way, when the loop finishes all its alerts, the keyboard focus goes to
someplace that doesn't care about it. Now you can safely hold down the
Enter key with fast repeats to get through all the alerts, and it'll
automatically stop after the last alert.

Kev

PS. Yes, obviously if you look for an Enter key on the body, do
something else. Sheesh.
 
L

Laurent Vilday

Chuck Anderson a écrit :
This seems like a very, very basic question, but I've Googled it quite a
bit and can not find an answer. Is there a way to manually abort that
script (other than closing my browser) or do I have to keep clicking OK
until the loop has run it's complete course?

If you can't rely on a better debugging system than alert box, this
alerter() i propose below may help you.
Instead of alert('my debug'); you will do alerter('my debug');

It's very simple :
- we have a flag (alerter.$) initially set to true
- if the flag is set, we redefine it with the result of a confirm box
(true => OK, false => CANCEL)
- else we dont want to be bothered by the message and stop here

So basically with this, you keep clicking on the OK button until you are
bored and click CANCEL to let it go without the browser bothering you
again :)

function alerter(t)
{
if ( alerter.$ )
{
alerter.$ = confirm(t);
}
}
alerter.$ = true;

Just need a button somewhere on the page to reset the flag to true and
reactivate the messages and you are set.

<input type="button" value="Reset debug" onclick="alerter.$=true;">

I already can hear peoples arguing to not use the $ character in this
situation, ok fine, replace it to anything else that suits you :)
 
C

Chuck Anderson

Laurent said:
Chuck Anderson a écrit :


If you can't rely on a better debugging system than alert box, this
alerter() i propose below may help you.
Instead of alert('my debug'); you will do alerter('my debug');

It's very simple :
- we have a flag (alerter.$) initially set to true
- if the flag is set, we redefine it with the result of a confirm box
(true => OK, false => CANCEL)
- else we dont want to be bothered by the message and stop here

So basically with this, you keep clicking on the OK button until you are
bored and click CANCEL to let it go without the browser bothering you
again :)

function alerter(t)
{
if ( alerter.$ )
{
alerter.$ = confirm(t);
}
}
alerter.$ = true;

Just need a button somewhere on the page to reset the flag to true and
reactivate the messages and you are set.

<input type="button" value="Reset debug" onclick="alerter.$=true;">

I already can hear peoples arguing to not use the $ character in this
situation, ok fine, replace it to anything else that suits you :)
I like it ... works for me.

As far as resetting alerter.$ to true (I called it alerter.on): I
already provide another button I call Refresh which does the same as
Reset but also submits the form (eliminating the previous search results
from the page - as though it was initially loaded before the form was
submitted). That also accomplishes the task of resetting alterer.on to
true.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,332
Latest member
LeesaButts

Latest Threads

Top