javascript 在同一浏览器的新标签页中阅读 PDF 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21524316/
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
Read PDF file in a new tab of same browser
提问by sandeep
In a HTML table I am showing a list of data containing pdf files. All the pdf file name are hyperlinks. When user clicks on that hyper link the pdf should open in a new tab. Basically on click of the hyperlink I am calling an JS function, which calls an method to the server side. In the server side scripting I have written the below code.
在 HTML 表中,我显示了包含 pdf 文件的数据列表。所有pdf文件名都是超链接。当用户单击该超链接时,pdf 应在新选项卡中打开。基本上点击超链接我正在调用一个 JS 函数,它调用一个方法到服务器端。在服务器端脚本中,我编写了以下代码。
wrapper = FileWrapper(file(file_name))
response = HttpResponse(wrapper, mimetype='application/pdf; charset=utf-8')
response['Content-Disposition'] = 'inline; filename=' + file
response['Content-Length'] = os.path.getsize(file_name)
return response
This code is working fine and on click of the link the file opens, but I want to open it in a new tab instead of the same tab. I have already checked with using target=_blank, but no luck. Is there any way we can do through content-"options"or any other solution.
这段代码工作正常,点击链接文件会打开,但我想在新选项卡而不是同一个选项卡中打开它。我已经使用target=_blank进行了检查,但没有运气。我们有什么办法可以通过内容-“选项”或任何其他解决方案来做。
I am using Djangoas my server side scripting.
我使用Django作为我的服务器端脚本。
回答by Emilio Rodriguez
You can open a new tab/window from your JavaScript and load the results in there:
您可以从 JavaScript 打开一个新选项卡/窗口并在其中加载结果:
function openPDF(url){
var w=window.open(url, '_blank');
w.focus();
}
<div onclick="openPDF('pdf/1.pdf');">PDF 1</div>
<div onclick="openPDF('pdf/2.pdf');">PDF 2</div>
回答by tominardi
You also can add a class on your links instead of using an inline event.
您还可以在链接上添加一个类,而不是使用内联事件。
It look like this, in jQuery :
它看起来像这样,在 jQuery 中:
$('a.extLink').click(function(){
window.open(this.href);
return false;
});