Monday, March 21, 2011

Twitter Popup Window for Custom Twitter button

Are you tired of having to open a new browser or tab to share a tweet from a custom button?
The "standard" Twitter share button allows you to use a popup window to share on Twitter but custom share buttons have not been able to do this without a lot of custom javascript.

Well I figured out a work around so that you can use existing Twitter libraries with your custom button. The code is based on the Twitter bookmarklet code with a small modification.
 I changed the source of the referenced javascript source from the bookmark.js to the widget.js --- and it worked. See green highlight.


FROM: E.src='http://platform.twitter.com/bookmarklets/share.js?v=1
 
TO:E.src='http://platform.twitter.com/widgets.js

Well here is the code to solve that problem.


<a href="javascript:(function(){window.twttr=window.twttr||{};var D=550,A=450,C=screen.height,B=screen.width,H=Math.round((B/2)-(D/2)),G=0,F=document,E;if(C>A){G=Math.round((C/2)-(A/2))}window.twttr.shareWin=window.open('http://twitter.com/share?url=http://www.nerdery.com&text=cool&via@dan_krueger','','left='+H+',top='+G+',width='+D+',height='+A+',
personalbar=0,toolbar=0,scrollbars=1,resizable=1');E=F.createElement('script');E.src='http://platform.twitter.com/widgets.js';F.getElementsByTagName('head')[0].appendChild(E)}());
"><img src="twitter-logo.png" border="0"></a>


The code in the yellow highlight is the part of the code to customize for your share details. This code is based on the Twitter bookmark code with a small modification.



So now you can get a popup window for sharing from a custom twitter share button

7 comments:

  1. So... the tweet button on this page does not open in a popup? LOL

    ReplyDelete
    Replies
    1. But... The script works perfectly. Thanks a ton!

      Delete
  2. what about the facebook popup?

    ReplyDelete
  3. Thanks dude! Greatly appreciated!

    ReplyDelete
  4. made my f* day
    thanks a lot dude

    ReplyDelete