Davidm113021
Davidm113021

Reputation: 81

Opening a new tab/window after an AJAX call

I need help making an ajax call and using the response to open a new tab; the code I have sort of works but if the function is called more than once, it will simply replace the contents of the first tab instead of opening another one.

function openInNewTab(url){
    let newTab = window.open("about:blank", "new_tab")
    $.ajax('api/path').done(function(response){
        newTab.location = url + '&data='+response;      
    })
}

How can I make it so a new tab will be opened each time the function is called?

Upvotes: 0

Views: 2470

Answers (1)

fishstick
fishstick

Reputation: 2274

Try this (url should be full qualified url, e.g. http://www.example.com/path/to/your/file/and/so/on)

function openInNewTab(url){
    $.ajax('api/path').done(function(response){
        window.open( url + '&data='+response, "_blank");    
    })
}

Upvotes: 1

Related Questions