Home Ask Login Register

Developers Planet

Your answer is one click away!

Davez01d February 2016

Adjust twitter button without reloading?

I have a random quote generator that tweets the random quote shown, everything works except the twitter button has to reload every time a new quote is generated and it doesn't look very good. Is there any way to change the iframe without the annoying reload?

link to codepen- http://codepen.io/Davez01d/pen/gPegpd

html -

<div class="row vert-space-25 vert-bottom-space-25 text-center"><!--twitterbutton-->
  <div id="twitter-button" class="col-xs-12">
    <a class="twitter-share-button" data-size="large">Tweet</a>
    <!--javascript for twitter button-->
    <script>window.twttr=(function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],t=window.twttr||{};if(d.getElementById(id))returnt;js=d.createElement(s);js.id=id;js.src="https://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);t._e=[];t.ready=function(f){t._e.push(f);};return t;}(document,"script","twitter-wjs"));</script>

javascript -

var quotes = [
  { text: '"She got a big booty so I call her big booty"', author: '2Chainz'}, 
  { text: '"Success consists of going from failure to failure without loss of enthusiasm."', author: 'Winston Churchill'}, 
  { text: '"If you\'re going through hell, keep going."', author: 'Winston Churchill'},
  { text: '"Success is not final, failure is not fatal: it is the courage to continue that counts."', author: 'Winston Churchill'},
  { text: '"Nearly all men can stand adversity, but if you want to test a man\'s character, give him power."', author: 'Abraham Lincoln'},
  { text: '"Whatever you are, be a good one."', author: 'Abraham Lincoln'},
  { text: '"I walk slowly, but I never walk backward."', author: 'Abraham Lincoln'},

$("#newQuote").click(function() { 
  var randomNum = Math.floor(Math.random() * quotes.length); //g        


ithcy February 2016

Nope. For security reasons, JavaScript on a page has no access to iframes on that page that reside on other domains. If you control both the host page and the page in the iframe, you can use postMessage to pass messages between them.

However, do you need to use the Twitter iframe button? There are other ways to share to Twitter with JavaScript. One simple way is just to compose a share URL: https://twitter.com/share?text=xxx&url=yyy

Where xxx is your tweet text and yyy is a URL to have the text link to. Be sure to url-encode both xxx and yyy.

So you can just use your own button or link, and accomplish the same thing.

Post Status

Asked in February 2016
Viewed 1,402 times
Voted 5
Answered 1 times


Leave an answer

Quote of the day: live life