2

I use Youtube subscribe button (https://developers.google.com/youtube/youtube_subscribe_button ).

Is there any way, while user clicks on it, to initiate my javascript function too?

Jonas
  • 121,568
  • 97
  • 310
  • 388
T.Todua
  • 53,146
  • 19
  • 236
  • 237
  • 1
    Interesting. Just to be clear, you've seen the Ts&Cs section? "You may not offer or promote prizes or rewards of any kind in exchange for clicking on a YouTube Subscribe button." We had a lot of questions a while ago where people wanted to gate access to a website on whether users had clicked 'like this on Facebook' but I haven't seen a YouTube version before! – Rup Sep 23 '13 at 23:23
  • how come [toneden.io](https://toneden.io) are doing it? – Skykid Felix Apr 18 '20 at 10:20

2 Answers2

4

You just need to add a data element with the function that you need to run.

<script>
function myEvent(e) 
{ 
    // do something here 
}
</script>

<div class="g-ytsubscribe" data-channel="GoogleDevelopers" data-onytevent="myEvent"></div>

https://developers.google.com/youtube/youtube_subscribe_button#Handling_Events_Demo

Kup
  • 882
  • 14
  • 31
  • The Subscribe Button's subscribe and unsubscribe events have been deprecated. Similarly, the `data-ytonevent` attribute, which could previously be added to the widget element to specify a listener for event notifications, has also been deprecated. — [source](https://developers.google.com/youtube/subscribe/reference) – Luke Feb 21 '18 at 00:04
1

just put the name of your function in an onclick like so:

<div id="whatever they make you call it" onclick="yourFunction();" />

or do this in javascript

document.getElementById("whatever it's called").onclick = "yourFunction();";

I'm not saying it's a good idea, and it will probably violate their terms of use, but this is what I would try.

milestyle
  • 931
  • 7
  • 14
  • 1
    The actual button is rendered inside an iframe inside the div, though. IIRC the click event won't be passed up to the div from that - although it looks like it's possible to [find the iframe and register a click handler on its content](http://stackoverflow.com/q/6452502). – Rup Sep 23 '13 at 23:37