fabien-d / alertify.js

JavaScript Alert/Notification System
http://fabien-d.github.com/alertify.js/
4.26k stars 725 forks source link

How to programmatically close prompt? #179

Closed Alex-Sokolov closed 7 years ago

Alex-Sokolov commented 11 years ago

I want to show user promt, and If he click OK open new page through window.open. But prompt stay showing. How to close it right way? Trigger click on Cancel-button is enough?

Alertify 0.3.11

fabien-d commented 11 years ago

Can you provide more info (browser, OS, steps to reproduce, code sample)? I've tried using the following code sample and am unable to recreate it:

alertify.prompt( 'test', function (e) {
    if (e) {
        window.open( 'http://google.com' );
    }
});
Alex-Sokolov commented 11 years ago

Windows 8, Google Chrome 30.0.1599.69 User click on link. Link to file will open in new tab, where when server generate file downloaded by browser and tab close. If all filled correctly opening new tab to file and it download without prompt. When not all filled user asks to download file anyway. When he click "Download" link opened in new browser tab, browser receive file and tab closing. We see page where click link but prompt already here too. Markup:

<table id="ref_table">
    <tr>
        <td>
            <h4>Download in</h4>
        </td>
        <td>
            <a class="link-class" id="ref_doc" href="/Document/Word">
                <img src="/Images/doc.png" alt="MS Word" />
            </a>
        </td>
        <td>
            <a class="link-class" id="ref_pdf" href="/Document/PDF">
                <img src="/Images/pdf.png" alt="PDF" />
            </a>
        </td>
    </tr>
</table>

JS

function downloadFile(event) {
    event.preventDefault();
    window.open($(this).attr('href'));
}

$('#ref_table').on('click', '.link-class', downloadFile);

// URL that check params, return true or false, in that case it is false        
$.getJSON('/check/', function (data) {
    if (data !== true) {
        $('#ref_table').off('click', '.link-class', downloadFile);

        $('#ref_table').on('click', '.link-class', function (event) {
            event.preventDefault();
            var $this = $(this);

            alertify.set({
                buttonReverse: true,
                buttonFocus: "cancel",
                labels: {
                    ok: "Download",
                    cancel: "Return"
                }
            });

            alertify.confirm("Not all filled correctly, download anyway?", function(e) {
                if (e) {
                    alertify.log('Start download');
                    $('#ref_table').on('click', '.link-class', downloadFile);
                    $this.trigger('click');
                }
            });

        });
    }
});