Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!