Zanic L3
Zanic L3

Reputation: 1088

Download a .pdf and redirect to another page

<a href="voucher.pdf">Download The Voucher</a>

So I have this voucher that the user can download but after that, it needs to redirect to another page. What would be a good way around this issue and something that is compatible on most browsers?

Upvotes: 0

Views: 1972

Answers (2)

denik1981
denik1981

Reputation: 380

Three steps for this:

1.Add the "download" attribute to the link tag which tells the browser that you don't want to follow the path to your pdf file but download it instead.

<a href="#" download>Download The Voucher</a>

2.Add an onclick event listener to your link tag.

const link = document.querySelector("a");
link.onclick = goSomewhere

3.Modify the window.location.href to your desire destination url

const goSomewhere = () => { window.location.href = "https://yourURL"}

Alternatively you can trigger a redirect using window.location.replace which in this case will send the proper HEADER HTTP request as if it were a normal redirect whereas changing the "href" will behave like a regular link.

Upvotes: 2

JvstAlf
JvstAlf

Reputation: 83

you could add onclick on <a>, something like

<a href="voucher.pdf" onclick="openTab()">Download The Voucher</a>

and on a js file or inside <script>

function openTab() {
    window.open('url');
}

on 'url' you can insert an external or internal page

Upvotes: 1

Related Questions