vendredi 10 juin 2016

Track down why window.open does not work


Other attempts to use window.open('url', '_blank') within this script work as desired. This one, not opening a new tab, uses different logic following a confirm() prompt. The logic is to display a portion of the page if 'Reserved' is found in it or to redirect to the target page otherwise.

The redirect is automatic as a result of the $.ajax success handler. Neither method of redirect makes any changes.

form for 2nd method:

<form method="get" id="formgoregistry" name="goregistry" action="" target="_blank">
</form>

javascript function in question:

function getRegistryInfo(num) {
    // for 2nd method, set the form's action
    $('#formgoregistry').attr('action', 'http://somesite.com/somefile?n=somevalue');

    var response = $.ajax({
        type: 'POST',
        url: 'purchases_registry.php',
        dataType: 'html',
        data: { number: num },
        success: function(data) {
            console.log('response = '+data);
            if (data.indexOf('Reserved') > 0) {
                //  work with the result
                //  Code in here works fine
                ....
            }
            else {
                // wrong data, redirect to outside page
                //  **  Neither of these method open a new tab
                // 1st method
                var url = 'http://somesite.com/somefile?n=somevalue';
                console.log('url = '+url);                // prints the correct url
                var w = window.open(url, '_blank');       // NO new tab or error
                console.log('window = '+w);               // window = undefined

                // 2nd method (setting form's action beforehand)
                console.log(document.goregistry.action);  // prints the correct url
                document.goregistry.submit();             // NO new tab or error
            }
        }
    });
}

I am wondering if the window being undefined is part of the problem and why.


Aucun commentaire:

Enregistrer un commentaire