Stop Javascript execution safely?

H

howa

Are there any method to stop the current JS execution on a page
safely, something like PHP's die() functuin ?
 
E

Erwin Moller

howa said:
Are there any method to stop the current JS execution on a page
safely, something like PHP's die() functuin ?

Hi,

From Javascript itself on the page?
As far as I know: No.

You could of course wrap every javascript-command and function in an
if-then block, checking if the command should run.

Why would you want such a thing in the first place?

Regards,
Erwin Moller
 
M

michael

Are there any method to stop the current JS execution on a page
safely, something like PHP's die() functuin ?

Hi Howa,

Yes javascript does come with error handling, and it work's just like
java.

try{
var myObj = document.missing.element;
} catch (e) {
alert('Error looking for document.missing.element\n' + e);
}

Hope it helps :)

//Michael
 
T

Thomas 'PointedEars' Lahn

Are there any method to stop the current JS execution on a page
safely, something like PHP's die() functuin ?

[...]
Yes javascript does come with error handling, and it work's just like
java.

It doesn't. At least not everywhere.
try{
var myObj = document.missing.element;
} catch (e) {
alert('Error looking for document.missing.element\n' + e);

If would have been easier to display the value of e.message or a similar
property.

That is falsely assuming error handling would be equivalent to stopping
execution with PHP's die(). To stop execution this way, one would have
to cause a run-time error deliberately, which is bad style, even in Java.
An example of a better way would be

var statements, foo = true;

try
{
statements;
if (foo) throw new Error("bar");
}
catch (e)
{
window.alert(e.message);
}

But one would have to rely on support for exceptions in either case here.

However, your reply indicates the very simple and fully compatible solution
to this actually minor problem: instead of stopping execution when a
condition applies, have the following code only executed if it doesn't.


PointedEars
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top