Results 1 to 6 of 6

Thread: how to stop javascript execution

  1. #1
    satis's Avatar
    Join Date
    Oct 2002
    Location
    Dallas
    Posts
    2,914
    Rep Power
    20

    how to stop javascript execution

    hi...this should hopefully be quick, but is there a javascript command to abort execution of the rest of the script? For instance...

    PHP Code:
    function whatever(var1){
        if(
    var1 == 'abort'){
            
    //command to kill program execution
        
    }
        
    //additional javascript commands

    A much simplified example. Anyway, I'm basically for something with the functionality of php's die() function.

  2. #2
    Mod Squad BerksWebGuy's Avatar
    Join Date
    Apr 2002
    Location
    Orlando, FL
    Posts
    6,972
    Rep Power
    24
    PHP die/exit will stop the page parsing (so nothing after that will be seen). Since javascript is on client-side, I don't think anything will stop the page from loading (unless you do a redirect).

    If you're talking about breaking a loop: http://developer.mozilla.org/en/docs...atements:break
    Or just put the commands in an if statement.

    I'm not a javascript guru...but I guess I don't know exactly what you want to do (stop loading the page or just the script).

  3. #3
    satis's Avatar
    Join Date
    Oct 2002
    Location
    Dallas
    Posts
    2,914
    Rep Power
    20
    yea, I saw the break to break the loop, but I'm trying to just stop the javascript function from continueing execution. I can work around it in other ways, but that would require alot of redundant code or another function, both of which I'd like to avoid.

    Basically, it's just a javascript function that can be called from multiple (dynamically generated) places, and if the data being passed into the function matches a specific value, I want to stop the function from continueing execution. I'm not really trying to stop page rendering or anything, I just don't want the rest of the code evaluated. This if for an AJAX-based page, hence why I'm trying to do all this in javascript.

    Anyway, I appreciate the assistance. Worst comes to worst, I'll just rebuild the function so I don't need to kill code execution.

  4. #4

    Join Date
    Jan 2005
    Location
    Northeast
    Posts
    185
    Rep Power
    13
    I'm not trying to be difficult, but what would happen if you just return from the function?

    If you return all the way back to whatever triggered Javascript execution in the first place (for example, the "onload" property of the <body> tag), then Javascript execution will stop.

    Or do you want to totally disable all event handlers after the condition is met?

    Or do I totally misunderstand....

  5. #5
    satis's Avatar
    Join Date
    Oct 2002
    Location
    Dallas
    Posts
    2,914
    Rep Power
    20
    heh...you may be spot on about returning. It didn't even occur to me. I'm gonna give that a shot.

    *edit* yep, that's what I was looking for. Just like me to try and figure out some hard way to do something rather than treating a function like a function.

  6. #6

    Join Date
    Jan 2005
    Location
    Northeast
    Posts
    185
    Rep Power
    13
    It's funny, because I thought I might be making a really stupid suggestion. I've never used AJAX (though I'm familiar with what it does), so it occurred to me that you might be having an AJAX-specific problem that would render my Javascript experience useless.

    Glad it worked out!

    -- Tom

Posting Permissions

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