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();
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?
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>
Please let me know if your problem has been solved by voting the answer :)