Catching Click event of hyperlink on PDF in IFrame
I'm loading pdf's into an ifram开发者_Python百科e on my web page by setting the src of the iframe to the path of the pdf document. This works as I'd like for viewing the pdf.
The problem is that some of the pdf's have hyperlinks in them to external websites which, when clicked, load the external website in my iframe.
So my question is: is it possible to catch the pdf's hyperlink click event, So that I could redirect the request to new page?
Thanks in advance for your valuable suggestions.
One idea: instead of setting the src to point to the PDF file directly, set it to point to a page that will load the pdf; e.g. http://example.com/getPdf.aspx?file=SOME_URI
The getPdf.aspx page can contain code that detects when the page is about to navigate and then perform some action. I would suggest that it calls the parent page which should first close the iframe and the navigate to the PDF.
Something like the following:
//In getPdf.aspx
function Navigate()
{
var parentWindow = this.parentNode.parentNode //this.parentNode.parentNode is the window hosting the iframe;
var parentWindow.CloseMe(this.parentNode, PDF_URI); //this.parent is the Iframe
}
window.onunload = Navigate;
//In main page
function CloseIt(window_to_close, pdf)
{
window_to_close.close();
window.navigate(pdf); // or window.location = pdf;
}
I haven't tested this but something very much like it might work. You might get a message warning that the script is trying to close the page or something.
Since the "PDF" Object is not part of HTML or DOM, it can not be listened by JavasScript listener. As I know, Adobe does not provide any method for us to listen to "hyperlink clicked" in PDF object. There seems no straight or easy way to accomplish your request.
But I have two suggestions for you:
Write you own reader. For example, you can read the PDF page in server side and render page to the client in a way that you can control (much like amazon book preview)
If you can ask your web site user to install browser plugin, I think it is possible to do more control over the PDF object.
Maybe it's possible to catch the event of the user navigating away using window.unload
.
window.onunload = function() {
/* Do something */
return false;
}
I can't vouch for the range of actions supported in all browsers in onunload
, however.
Something else to consider would be trying to set a default target
for URL's that opens in a new window (overriding this for links you want to behave normally). I am not entirely certain if you can set this behavior for an iFrame
in the parent document, however.
精彩评论