Home>
I want to achieve

We have set up a share button on Twitter and facebook on the site.
There are some actions that I want to execute in common when I click those buttons. How should I implement them?

The same process is executed regardless of whether you press the Twitter button or the Facebook button.

What I tried Added onclick attribute to parent element div

I surrounded the Twitter and facebook buttons with div tags and added an action with onclick to the div tag, but the onclick process is not executed when the button is pressed.
If i click a little outside the button, the onclick process will be executed.
The actual code looks like this:

<!-←← Process I want to execute->
    
      <!-Twitter button->
         Tweet 
      <script async src = "https://platform.twitter.com/widgets.js" charset = "utf-8"></script>
    
    
      <!-Facebook button->
         
      <script async defer crossorigin = "anonymous" src = "https://connect.facebook.net/ja_JP/sdk.js#xfbml=1&version=v8.0" nonce = "OooKwPjc"></script>
        
             Share 
        
    
Add onclick attribute to iframe with JavaScript

Also, it seems that Twitter and facebook buttons are generated by JavaScript, so
Looking at the generated one, it was as follows.

<!-Generated by Twitter JavaScript->

Apparently, the button is generated in it in the iframe.
So, I wrote the code to operate the onclick element of iframe using JavaScript, but this also does not work.

<script>
    document.getElementsByTagName ('iframe') [0] .onclick = function () {
        alert ('foooooooo');
    };
    document.getElementsByTagName ('iframe') [1] .onclick = function () {
        alert ('foooooooo');
    };
</script>
  • Answer # 1

    Doesn't it react unless it is written on the iframe side?

Related articles