Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

HTML Click handler for <embed> element

cbreemer

Bronze Coder
I have a page with an <embed> element that is displaying PDF files:

<embed id="Preview" type="application/pdf">

It works great but for some arcane reason I cannot put an onClick handler on this element. Well I can, but the function is never called.
To overcome this I had overlayed the <embed> with an <a> element and put the click handler on that anchor. This works beautifully, only I just noticed that now the mouse wheel no longer works to scroll the PDF.
Bit of a conundrum. Why can't I have it both ways ?

Anybody know why <embed> does not execute its mouse handler ? I would assume it's doing its own handling, but nothing at all happens when I click the mouse in it, so they might as well have let the event bubble up. Hmmm 🤔
 
Sounds like the PDF is actually getting the click and not the embed. Can you achieve what you want with a button under the embed?
I'm not sure how the PDF would be "getting the click", and how it would react to it, being just a file without code (or so I assume, you never know...).
Anyway yes, I could find something else to click on if the <embed> does want to play ball. There's always another way. I'd just like to understand why it does not seem possible.
 
You must understand that the PDF is not being displayed by your browser, but rather by a PDF viewer application which the browser has invoked. That application has established a handler for click events in order to implement the dynamic functionality supported by PDF. That is PDF is not "just a file without code"/ Note that is a reason why you should never open a PDF attachment to an e-mail unless you previously requested the known originator to send you a PDF. For example if you had a link in the PDF, you could click on that link. The trick is to overlay a transparent div tag over the embed tag. But remember that means that your users cannot access the PDF document itself. Alternatively you could export the PDF to JPEG, which has no local function of its own.
 
What others have said is 100% true.

A PDF (or most content in an embed) is going to need click/scroll/hover/etc events. This overrides the default site, because you are EMBEDDING a different "experience" (pdf, etc) into your site. The reason it doesn't work is because it's counterintuitive to have an embed that you would have a separate click event on.

Are you 100% sure an embed is what you want? What's the purpose of the click event btw? If you can share a bit more info I'm sure we can help you come up with some great UX that makes sense for your use case without breaking your PDF interactivity :)
 
You must understand that the PDF is not being displayed by your browser, but rather by a PDF viewer application which the browser has invoked. That application has established a handler for click events in order to implement the dynamic functionality supported by PDF. That is PDF is not "just a file without code"/ Note that is a reason why you should never open a PDF attachment to an e-mail unless you previously requested the known originator to send you a PDF. For example if you had a link in the PDF, you could click on that link. The trick is to overlay a transparent div tag over the embed tag. But remember that means that your users cannot access the PDF document itself. Alternatively you could export the PDF to JPEG, which has no local function of its own.
Thanks for that. Yes I guess there is more to PDF than I reckoned with. Overlaying a <div> is exactly what I now have to make the click possible.
 
What others have said is 100% true.

A PDF (or most content in an embed) is going to need click/scroll/hover/etc events. This overrides the default site, because you are EMBEDDING a different "experience" (pdf, etc) into your site. The reason it doesn't work is because it's counterintuitive to have an embed that you would have a separate click event on.

Are you 100% sure an embed is what you want? What's the purpose of the click event btw? If you can share a bit more info I'm sure we can help you come up with some great UX that makes sense for your use case without breaking your PDF interactivity :)
Thanks @Ghost . Yes I know it's a plugin handling the PDF, not the browser itself. My pdf's are invoices, manuals, official documents and such. I don't need interactivity, just the ability to view them. For illustration a screenshot of my page. Documents can be displayed filtered by kind, name and/or text content, a preview of the currently selected document is shown in an <embed> element, and clicking on that (or rather, on the overlayed <div>) will show the document in a new window. I'm very happy with that setup except for the small niggle that I cannot use the mouse wheel in the <embed>.
a.jpg
 
Thanks @Ghost . Yes I know it's a plugin handling the PDF, not the browser itself. My pdf's are invoices, manuals, official documents and such. I don't need interactivity, just the ability to view them. For illustration a screenshot of my page. Documents can be displayed filtered by kind, name and/or text content, a preview of the currently selected document is shown in an <embed> element, and clicking on that (or rather, on the overlayed <div>) will show the document in a new window. I'm very happy with that setup except for the small niggle that I cannot use the mouse wheel in the <embed>.
View attachment 2594
Ah yeah, this is tricky.

What I suggest should work here:
- on the div, add CSS "pointer-events: none;" on your overlay (transparent) div. This should let scrolls pass through the div, into the PDF.

- but keep the click event in place

Let me know if this works :)
 
  • on the div, add CSS "pointer-events: none;" on your overlay (transparent) div. This should let scrolls pass through the div, into the PDF.
  • but keep the click event in place

Let me know if this works :)
Thanks, that seemed worth a try. But alas it does not make any difference whatsoever.

But it's fine, I am quite used to how it works by now. What I mostly do anyway is click on the preview and then view the document in the new window, where everything just works. So it's quite a moot point really, let's drop it. Thanks for your help though !
 
Ah yeah, this is tricky.

What I suggest should work here:
- on the div, add CSS "pointer-events: none;" on your overlay (transparent) div. This should let scrolls pass through the div, into the PDF.

- but keep the click event in place

Let me know if this works :)

Removing pointer-events will stop the click as well. Here is a test:

HTML:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        button{
            padding: 20px;
            outline:1px solid grey;
        }
        .nopointerevents{
            padding: 20px;
            outline:1px solid grey;
            pointer-events: none;
        }
    </style>
</head>
<body>
    
    <button onclick="myFunction()">Click me!</button>
    <button class="nopointerevents" onclick="myOtherFunction()">Click me too if you can!</button>
    
    <p id="demo"></p>
    <p id="demotoo"></p>
    
    <script>
        function myFunction() {
              document.getElementById("demo").innerHTML = "Hello World";
        }
        function myOtherFunction() {
              document.getElementById("demotoo").innerHTML = "No dice!";
        }
    </script>
</body>
</html>
 

New Threads

Buy us a coffee!

Back
Top Bottom