• onAfterClick

    Mar 10 2006

    Okay, some time ago I posted onAfterPaste, a way to run some code after pasting. You can actually do the same thing with any event. Let's say you want to put an onclick handler on a submit button, but you don't want the function to execute until after the form is submitted. You may want to close the window after launching a new window or submitting a form, but putting window.close() in the onclick would prevent the form from being submitted. Then, do this:

    function onAfterClick (e) {
         window.close();
    }
    
    var submit= document.getElementById('submit');
    submit.onclick = function(e) {
         setTimeout(function() {
              onAfterClick(e);
         }, 1);
    }

    That's it. It's like the browser processes the click handler, then goes and does it's default click behaviour, then executes the timeout function. So it's almost a kind of magic.. but not really. Anyway, setTimeout is a great tool for making things happen at the right time.

  • Comments

    1. Paul at 6:26am on June 8, 2007

    Thanks for this.

    I needed to create a popup under (hideous I know) on a site when the user clicks the submit button. Firefox refuses to submit the form an onclick after window.open was called, your  method works a treat.

    Cheers

    Commenting is now closed. Come find me on Twitter.