Stop Javascript execution safely?

Discussion in 'Javascript' started by howa, Sep 6, 2007.

  1. howa

    howa Guest

    Are there any method to stop the current JS execution on a page
    safely, something like PHP's die() functuin ?
     
    howa, Sep 6, 2007
    #1
    1. Advertising

  2. howa

    Erwin Moller Guest

    howa wrote:
    > 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
     
    Erwin Moller, Sep 6, 2007
    #2
    1. Advertising

  3. howa

    Guest

    exit(0);
     
    , Sep 6, 2007
    #3
  4. wrote:
    > exit(0);


    troff


    PointedEars
    --
    Anyone who slaps a 'this page is best viewed with Browser X' label on
    a Web page appears to be yearning for the bad old days, before the Web,
    when you had very little chance of reading a document written on another
    computer, another word processor, or another network. -- Tim Berners-Lee
     
    Thomas 'PointedEars' Lahn, Sep 6, 2007
    #4
  5. howa

    Guest

    On 6 Sep., 19:12, howa <> wrote:
    > 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
     
    , Sep 7, 2007
    #5
  6. wrote:
    > On 6 Sep., 19:12, howa <> wrote:
    >> 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
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
     
    Thomas 'PointedEars' Lahn, Sep 7, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack Wright
    Replies:
    1
    Views:
    978
    Jack Wright
    Feb 22, 2004
  2. Jack Wright
    Replies:
    2
    Views:
    6,930
    yeswanthi
    May 28, 2007
  3. Gopal Krish

    Stop further execution in Page_Load

    Gopal Krish, Oct 24, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    13,008
    Gopal Krish
    Oct 25, 2004
  4. TS
    Replies:
    2
    Views:
    4,287
    Steven Cheng[MSFT]
    Jan 23, 2006
  5. Ralphz

    How do I stop javascript execution?

    Ralphz, Sep 19, 2007, in forum: Javascript
    Replies:
    3
    Views:
    180
    The Natural Philosopher
    Sep 19, 2007
Loading...

Share This Page