开发者

PDF document hyperlink javascript to alter parent window URI

I have an HTML web page (the parent) that contains a hyperlink. When clicked, it opens a new window and displays another HTML document (the child):

 <a href="/testhtml.html" target="_blank">Open Child</a>

Inside the child document are several links that are constructed like this:

 <a href="#" onclick='opener.window.location.href="/somewhere.html";
                   window.close();'>Make Parent Go Somewhere</a>

So the effect is that you can click on a link in the child window, it will cause the parent window to go to that new uri, and then close itself. This works wonderfully.

I'd like to do that same thing with a PDF as the child document. I can embed hyperlinks in the PDF just fine, or even embed Javascript:

  13 0 obj
  <<
  /Type /Action
  /S /JavaScript /JS
    (opener.window.location.href="/somewhere.html";)
  >>
 endobj

The Javascript gets triggered nicely, but the object "opener" is not defined. (ReferenceError: opener is not defined 1: Link:Mouse Up) Is what I'm looking to do even possible? What would be the object 开发者_C百科I would use to access the opening window's uri?

PS: If it's a problem, I do have some control over the user's target environment. I can specify that they use Acrobat Reader, and even a later version of it.


HTML and PDF don't use the same object model. None the less, it's possible to get HTML and PDF JS to talk to one another:

Getting notified when the user clicks a link in an embedded PDF


One easy way of achieving your goal is to create a PDF hyperlink with href="javascript:myfunction()", and defining myfunction() in your HTML web page. The resulting HTML file would be something like:

<html>
<head>
 <script type="text/javascript">
    function myfunction(){
        alert("captcha!");
    }
 </script> 
</head>
<body>
<object data="myfunction.pdf" type="application/pdf" width="100%" height="100%">
</object>
</body>
</html>

And your hyperlink PDF object would be:

<</Type /Action /S /URI /URI (javascript:myfunction\(\))>>

Inside myfunction() you will be able to use the HTML object model as usual.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜