最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to dynamically change the "src" or "data" for a PDF ObjectEmbed file using JavaScript? -

programmeradmin2浏览0评论

I have a web application that is dynamically loading PDF files for viewing in the browser. Currently, it uses "innerHTML" to replace a div with the PDF Object. This works.

But, is there a better way to get the ID of the element and set the "src" or "data" parameter for the Object / Embed and have it instantly load up a new document? I'm hoping the instance of Adobe Acrobat Reader will stay on the screen, but the new document will load into it.

Here is a JavaScript example of the object:

document.getElementById(`divPDF`).innerHTML = `<OBJECT id='objPDF' DATA="'+strFilename+'" TYPE="application/pdf" TITLE="IMAGING" WIDTH="100%" HEIGHT="100%"></object>`;

Any insight is appreciated.

I have a web application that is dynamically loading PDF files for viewing in the browser. Currently, it uses "innerHTML" to replace a div with the PDF Object. This works.

But, is there a better way to get the ID of the element and set the "src" or "data" parameter for the Object / Embed and have it instantly load up a new document? I'm hoping the instance of Adobe Acrobat Reader will stay on the screen, but the new document will load into it.

Here is a JavaScript example of the object:

document.getElementById(`divPDF`).innerHTML = `<OBJECT id='objPDF' DATA="'+strFilename+'" TYPE="application/pdf" TITLE="IMAGING" WIDTH="100%" HEIGHT="100%"></object>`;

Any insight is appreciated.

Share Improve this question edited Sep 23, 2008 at 18:45 asked Sep 18, 2008 at 19:11 Tristan PutmanTristan Putman
Add a ment  | 

5 Answers 5

Reset to default 1

I am not sure if this will work, as I have not tried this out in my projects.

(Looking at your JS, I believe you are using jQuery. If not, please correct me)

Once you have populated the divPDF with the object you might try the code below:

$("objPDF").attr({
    data: "dir/to/newPDF"
});

Again, I am not sure if this will work for your particular needs but if you attach this code to an event handler you can switch out the data of the object.

You could also wrap it in a function to be used over and over again:

function pdfLoad(dirToPDF) {
    $("objPDF").attr({
        data: dirToPDF
    });
}

If the handler for the PDF is acrobat (it doesn't have to be), it exposes a JS interface that is documented here:

http://www.adobe./devnet/acrobat/pdfs/js_api_reference.pdf

See if you can call openDoc(urlToPdf) on document.getElementById('objPDF') -- even if this works, it only works when Acrobat is being used to handle 'application/pdf'

@lark A slight correction:

$('#objPDF').attr('data','dirToPDF');

The # specifies the objPDF is an ID and not an element name. Though I still don't know if this will work.

@Tristan Take a look at the jQuery Media plugin. It mentions support for PDF as well, though I have never used it.

Open a PDF-Link in a external window PDFN with a external PDF-Reader.EXE:

Clicking on the following button:

<FORM action=""> 
    <INPUT type="button" value="PDF file" 
        onclick="window.open('http://www.Dku-betrieb.eu/Pdfn.html', 
        'PDFN', 'width=620, height=630')">
</FORM>

opens this frameset Pdfn.html in an external window:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3/TR/html4/frameset.dtd">
<html lang="de">
    <meta http-equiv="refresh" content="12;url=http://www.dku-betrieb.eu/Pdfn1.html">
    <head>
        <title>Reader</title>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <frameset>
    <frame src="http://www.dku-betrieb.eu/File.pdf" frameborder=0 name="p1">
    </frameset>
</HTML>

which refreshes in 12 seconds to the download of the PDF-Reader:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3/TR/html4/frameset.dtd">
<html lang="de">
    <head>
        <title>Reader</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <frameset >
    <frame src="http://www.dku-betrieb.eu/PDFReader.exe" frameborder=0 name="p2">
    </frameset>
</HTML>

showing as result the PDF-file in the external window PDFN.

function pdfLoad(datasrc) {

          var x =  document.getElementById('objPDF');
          x.data = datasrc;

        }

This worked for me

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论