How to open new tab page when ajax is successful

Hello,

When I click with the code below, a new tab opens without being blocked

<a onclick="pdf_dow('201')">PDF</a>
  
  function pdf_dow(pdf_dow_id){
	window.open('../pdf.php?pdf=' + pdf_dow_id, '_blank');
  }

However, when I click the url with below code the ajax is working and when the ajax is successful I want it to open the new tab page but it is blocked

How to open new tab without being blocked?

<a onclick="create_pdf('201')">PDF</a>

    function create_pdf(id){
        $.ajax({
          type: "GET",
          url: "filename.php",
          data: {"pdfid" : id },
          success: function(){
            pdf_dow(id);
          }
        });
      }

I searched a lot on the internet, tried some suggestions, but was unsuccessful.

I tried to do it with native JS and it’s working but I don’t know how to do it with jquery

<input type="button" id="btn" value="get user" onclick="getUser(1)" />
function getUser(id) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    if(xhr.readyState == 4)
        if(xhr.status == 200) {
            goToView(id)
        }
}
xhr.open('GET', `view.php?id=${id}`);

xhr.send();
}

function goToView(id) {
    window.location = `view.php?id=${id}`
}

Thank you for the answer
But I want to open new tab

ahh, ok well It’s working for me using this native js snippet It opens in a new tab


function getUser(id) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4)
            if (xhr.status == 200) {
                goToView(id)
            }
    }
    xhr.open('GET', `view.php?id=${id}`);

    xhr.send();
}

function goToView(id) {
    window.open(`view.php?id=${id}`, '_blank');
}

Thank you again
However, the browser is blocking
I want to bypass browser blocking but that is not possible I guess

It informs the visitor with the following code
I’ll try to deal with it for now

var win = window.open('http://stackoverflow.com/', '_blank');
if (win) {
    //Browser has allowed it to be opened
    win.focus();
} else {
    //Browser has blocked it
    alert('Please allow popups for this website');
}

I have the pop-ups blocked but it’s working normally on pc and phone. Did you try creating a link and simulating the click? using .click()

I don’t know how to do this
When I click it manually there is no problem, the tab opens.
However, when ajax is successful, automatically opening tabs is blocking the browser.

What is the overall goal you are trying to accomplish, i.e. why doesn’t the download processing in pdf.php create the pdf when needed?

Sponsor our Newsletter | Privacy Policy | Terms of Service