Chris
Chris

Reputation: 441

How to get anchor text from twitter follow button in javascript / jquery?

I am trying to get my twitter follower count to show on my website. I am planning to have have it as an animated counter.

I know you can do this through the twitter API, but I'm having difficulty understanding the documentation (very limited coding skills). It also seems like overkill when I already have my twitter follower count, in text format, loading next to a follow button on my website.

I thought it would be relatively easy to get the anchor text from the link the twitter code creates. Nope

The twitter follow button code looks like this:

<a href="https://twitter.com/TwitterDev" class="twitter-follow-button" data-show-count="true">Follow @TwitterDev</a>
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>

When the page loads, part of what it creates is an anchor tag with your follower count, that looks like this:

<a id="count" class="note" href="https://twitter.com/intent/user?ref_src=twsrc%5Etfw&amp;region=count_link&amp;screen_name=TwitterDev&amp;tw_p=followbutton">99999 followers</a>

When I try to get the text from an example anchor tag, it works fine. But as you can see in this jsfiddle, when I try get the text from the twitter button, it returns as undefined.

https://jsfiddle.net/d1zt3d6x/3/

I have added a delay to make sure the button has loaded be before running the code. I'm out of ideas. Any help would be greatly appreciated!

EDIT: Apologies there was an error in the jsfiddle. Now corrected.

Upvotes: 0

Views: 127

Answers (1)

C3roe
C3roe

Reputation: 96306

The Twitter button is replaced by an iframe by the widgets.js script, and that iframe loads content from Twitter’s domain - which means you have absolutely no access to it via JavaScript, because the Same Origin Policy forbids that.

What you want is not possible.

Upvotes: 1

Related Questions