简体   繁体   English

下载 a.pdf 并重定向到另一个页面

[英]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?什么是解决这个问题的好方法以及在大多数浏览器上兼容的东西?

you could add onclick on <a>, something like您可以在 <a> 上添加 onclick,例如

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

and on a js file or inside <script>并在 js 文件或内部 <script>

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

on 'url' you can insert an external or internal page'url'上,您可以插入外部或内部页面

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. 1.将“下载”属性添加到链接标签,告诉浏览器您不想跟随 pdf 文件的路径,而是下载它。

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

2.Add an onclick event listener to your link tag. 2.将onclick事件监听器添加到您的链接标签。

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

3.Modify the window.location.href to your desire destination url 3.将 window.location.href 修改为您想要的目的地 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.或者,您可以使用window.location.replace触发重定向,在这种情况下,它将发送正确的 HEADER HTTP 请求,就像它是一个普通的重定向一样,而更改“href 链接”将表现得像一个普通的重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM