Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
Create and click A element with target blank

I need to create a new <a> element with the download property and target blank. This link must not be visible in html and will be clicked with JavaScript.

let a = document.createElement("a");
document.body.appendChild(a);
a.setAttribute("style", "display: none");
a.href = blobUrl;
a.target = "_blank";
a.download = "MyFileName.pdf";
a.click();

Result in HTML: enter image description here

The problem is that the target blank seems to be ignored. It worked until recently and there have been no changes. What could be the problem?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try this, and also please move the document.body.appendChild(a); to the end just like what I have done.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>


<script>

    function createA(){
        let a = document.createElement("a");
        a.setAttribute("style", "display: none");
        a.href = "#";
        a.target = "_blank";
        a.download = "MyFileName.pdf";
        a.click();
        a.innerHTML = "SomeText";
        document.body.appendChild(a);
    }
    
    createA();
</script>

</body>
</html>

The final result for me Please let me know if your problem has been solved by voting the answer :)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda