javascript 打开一个新选项卡并同时下载 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30624441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:26:30  来源:igfitidea点击:

Open a new tab and download a PDF at the sametime

javascriptjqueryhtmlcsspdf

提问by Sam

On clicking a PDF download link, I'd like the pdf to be displayed in that same tab and in a second tab the page /case-studies to load.

单击 PDF 下载链接时,我希望pdf 显示在同一个选项卡中,并在第二个选项卡中显示页面 /case-studies to load

Is this possible?

这可能吗?

Currently I've got:

目前我有:

<h2 style="text-align: center;"><a href="http://cdn2.hubspot.net/hub/155045/file-847580737-pdf/Stepping_into_a_new_age_of_marketing_with_CRM_FINAL_APPROVED.pdf" id="pdflink" target="_blank" download=""><strong>Click here to download your eBook</strong></a></h2>
<script type="text/javascript">// <![CDATA[
document.getElementById("pdflink").onclick = function () {
? ? ? ? location.href = "https://www.workbooks.com/case-studies";
? ? };
// ]]></script>

However this opens the redirect in a second tab but as the main window confusing the visitor (as if on IE or FF) they don't release the PDF has opened in the first tab.

但是,这会在第二个选项卡中打开重定向,但由于主窗口使访问者感到困惑(就像在 IE 或 FF 上一样),他们不会发布在第一个选项卡中打开的 PDF。

Any advice?

有什么建议吗?

回答by m4lt3

You should do it this way (during the onClick event):

您应该这样做(在 onClick 事件期间):

window.open("https://www.workbooks.com/case-studies",'_blank');