SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to "stop" a script from executing?

    I've looked around, but I can't seem to figure this out.

    I simply want to have a conditional exit in my javascript..

    ie:

    if (some condition) {
    exit the script}

    Is it possible to do this?

    I've found return, break, etc.. but I can't find an exit command.

    Thanks!
    - A simple online WYSIWYG editor for HTML code snippets.
    - Managed Web Hosting - $3.95/month (resellers welcome)
    - Why pay more? $8.95 domains & $9.95 SSL certificates!

  2. #2
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Break should do what you want. That will stop the script cold ...
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  3. #3
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Conditional nesting

    Although not what you were asking, and i agree completely with Maelstrom's answer, it might also be worth noting that JS no longer needs nesting in singular statements.

    if (killme) break;

    This still works to break the system.

    It actually works by stopping the parser at the current breakpoint (i believe).

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  4. #4
    SitePoint Addict Philip Toews's Avatar
    Join Date
    Dec 2001
    Location
    Kuala Belait, Brunei
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    are you guys sure about this?

    I've tried placing break in an if statement but get an error message saying I can't use it outside of a loop.

    So the following won't work

    PHP Code:
     function correct() {
    if (
    alreadyanswered == false){
    alert ("alreadyanswered= " alreadyanswered "The question has not yet been answered");
    alreadyanswered true;
    alert("Correct!  var alreadyanswered= "alreadyanswered); 
    }else{
    alert("This question has already been answered");
    break();
    }

    Philip Toews Professional esl Educator and ASP.NET wannabe

    http://www.philiptoews.com
    philip@philiptoews.com

  5. #5
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    break wont exactly stop a script from executing as this will continue to goto an outside loop, so an if statement inside a for loop would continue until the for loop is finished, you may want to try 'return' as this will pass the variable back to the place it was called and stop the function.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •