<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Embedding a PDF using PDFObject: Conditional JavaScript invoked upon successful embed</title> <!-- This example created for PDFObject.com by Philip Hutchison (www.pipwerks.com) --> <!-- CSS for basic page styling, not related to example --> <link href="/css/examples.css" rel="stylesheet" type="text/css" /> <style type="text/css"> <!-- #pdf { } #pdf.embedded { width: 500px; height: 300px; margin: 2em auto; border: 10px solid #6699FF; } #pdf p { padding: 1em; } #pdf object { display: block; border: solid 1px #666; } #statement { padding: 1em; margin: 1em; border: solid 4px #FFF; font-size: medium; font-weight: bold; } .success { background: #00CC33; color: #FFFFFF; } .fail { background: #FF3366; color: #FFFFFF; } --> </style> <script type="text/javascript" src="/downloads/pdfobject_source.js"></script> <script type="text/javascript"> /* PDFObject returns a reference to the JavaScript object (pseudo 'class' instance). PDFObject.embed() returns a reference to the embedded HTML <object>. Knowing this, we store the references in two separate variables and perform actions with them. For this example, We'll store the PDFObject obj in 'pObj' and the HTML object in htmlObj. */ var pObj = new PDFObject({ url: "/pdf/sample.pdf", id: "myPDF", pdfOpenParams: { navpanes: 0, toolbar: 0, statusbar: 0, view: "FitV" } }); //Note: the PDF has NOT been embedded yet because .embed() has not been invoked. function embed(){ var s = document.getElementById("statement"); var msg; document.getElementById("pdf").className = "embedded"; //Style the DIV var htmlObj = pObj.embed("pdf"); //Returns a reference to the HTML <object> or null if unsuccessful if(htmlObj){ msg = "The PDF was successfully embedded!"; s.className = "success"; } else { msg = "It appears the embed didn't work."; s.className = "fail"; } s.innerHTML = msg; } window.onload = function (){ var s = document.getElementById("statement"); var msg; if(pObj){ var plugintype = pObj.get("pluginTypeFound"); msg = "Type of PDF plugin found: " +plugintype; if(plugintype){ msg += "<br/><a href='#' onclick='embed(); return false;'>Click here to embed the PDF!</a>"; } else { msg += "<br/>Looks like we won't be able to embed your PDF! Doh!"; s.className = "fail"; } } s.innerHTML = msg; }; </script> </head> <body> <h1>Embedding a PDF using PDFObject:<br /> Conditional JavaScript invoked upon successful embed<br /> (Advanced example)</h1> <p id="statement">This example uses a few lines of JavaScript wrapped in a <em>window.onload</em> statement, with a little CSS added to control the styling of the embedded element.</p> <div id="pdf"></div> <p><a href="/index.php">« Return to PDFObject home</a></p> </body> </html>