簡體   English   中英

如何在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('');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM