如何在javascript中创建指向pdf的链接
How do I create a link to a pdf in javascript?
我是一个使用XHTML模板创建电子商务网站的新手。我想在某些项目下添加免责声明,但不是全部。为了避免在每个项目下键入免责声明(并避免将来在免责声明更改时出现问题),我想使用 javascript 创建一个副本块,当我指向它时,会添加免责声明。我已经成功地做到了(是的!),但是,在免责声明中有一个指向pdf的链接。当我使用 html 链接到 pdf 时,它失败了。我知道这可能是因为我没有正确的语法,因为 html 在 javascript 代码的"内部"。有人可以帮忙吗?
这是我所拥有的:
//<![CDATA[
function openPDF(file)
{
window.open (file, 'resizable,scrollbars');
}
//]]>
</script>
<script type="text/javascript">
//<![CDATA[
onload=function()
{
var txt=document.getElementById("myDiv")
txt.innerHTML="Photos do not represent actual size. Refer to measurements for sizing.
Measurements are approximate. Colors shown may differ depending on your computer
settings. Colors described are subjective and colors may vary from piece to piece,
depending on the natural properties of the stones. To learn more, see our <a href="#"
onClick="openPDF('www.shop.site.com/media/JewelryGuide.pdf')">Jewelry Guide</a>.";
}
//]]>
</script>
这是我用来调用它的代码:
<div id="myDiv"></div>`
嗨,像下面这样的函数可以完成我相信的工作。.
function openPdf(e, path) {
// stop the browser from going to the href
e = e || window.event; // for IE
e.preventDefault();
// launch a new window with your PDF
window.open(path, 'somename', ... /* options */);
}
嗨,我做了一个小提琴,希望它有帮助...http://jsbin.com/aQUFota/1/edit
1)该文本字符串中有换行符。2)你需要转义几个引号。我选择交换引号并转义文件名周围的引号。
txt.innerHTML = 'Photos do not represent actual size. Refer to measurements for sizing. Measurements are approximate. Colors shown may differ depending on your computer settings. Colors described are subjective and colors may vary from piece to piece, depending on the natural properties of the stones. To learn more, see our <a href="#" onClick="openPDF(''www.shop.site.com/media/JewelryGuide.pdf'')">Jewelry Guide</a>.';
如果你不想要一长行,你可以像这样分解这些行:
txt.innerHTML = 'Photos do not represent actual size.' +
'Refer to measurements for sizing. Measurements are approximate.' +
'Colors shown may differ depending on your computer settings.' +
等。
甚至:
txt.innerHTML = [
'Photos do not represent actual size.',
'Refer to measurements for sizing. Measurements are approximate.',
'Colors shown may differ depending on your computer settings.'
].join('');
相关文章:
- jqGrid-单击链接时在新窗口中显示PDF
- 将屏幕截图显示为pdf下载链接
- 调整从 aspx 页面链接的 pdf 的大小
- 打开打印窗口处于活动状态的 PDF 链接
- PHP/HTML:下载PDF链接处理页面加载
- Chrome 原生 PDF 阅读器在新标签页中打开超链接
- IE javascript 书签let PDF 链接不起作用
- 如何在javascript中创建指向pdf的链接
- 给定指向 html 代码中的 pdf 文件的链接.但它无法打开它
- 生成嵌入 PDF 的直接链接
- PDF中的自定义操作链接.编写Javascript以更改单击时链接的外观
- 此脚本将特定属性应用于网站上的所有链接.如何排除pdf和zip文件
- 如何查找涉及PDF's,然后在链接的前面附加一个绝对路径
- 如何使用jsPDFjs库在PDF中创建超链接
- PDF的SSRS链接失败
- 下载所有外部链接如(pdf/images)必须自动下载
- 除了pdf以外的所有文件链接的JS RegEx
- HTML嵌入的PDF所有链接覆盖在一个新的选项卡中打开(target="_blank")
- 在新选项卡中从文档库视图中打开PDF链接
- 自动添加图像到PDF链接在WooCommerce产品标签