Search code examples
javascriptform-submitnew-window

Is there a way to submit a form, and then open a new window?


I'd like to be able to do this without using window.open because I've been told that gets caught by popup blockers. Would it be possible to use javascript to alter the submit button in some way (like add a target="_blank") or something to achieve this?


Solution

  • Your new window will not get blocked by the browser if window.open was called as a result of user action.

    Example:

    // Find the button you want to bind to 
    var button = document.getElementById('test');
    // Add a click event listener
    button.addEventListener('click', function(){
        window.open('http://www.google.com'); 
    });
    

    All major browsers will allow the above to open a new window / tab.

    JSFiddle