Home Tutorials Forums Articles Blogs Movies Library Employment Press
By Haik Sahakian
|
Published
Here's Actionscript code for a "Tweet This" button for Twitter, or "Like" or "Recommend" button for Facebook. When the button is clicked, call the socialUpdate() function below, passing in the text and link that you wish to post. For example:

socialUpdate("twitter", "Views from space", "http://www.esa.int/esaHS/SEMG2856JGG_index_mg_1.html");



Both the text and the link can be blank. Here's the code to include:

function socialUpdate(site:String, text:String = "", link:String = "")
{
    // initialize
    var siteURL:String;
    var maxLength:Number;
    switch (site)
    {
        case "facebook":
            maxLength = 255;
        break;
        case "twitter":
            maxLength = 140;
        break;
        default:
            return;
        break;
    }
   
    // truncate passed text if necessary
    var availableTextLength:Number = maxLength - (link.length + 1);
    if (text.length > availableTextLength)
    {
        text = text.substr(0, (availableTextLength - 3)) + '...';
    }
   
    // construct url to site
    switch (site)
    {
        case "facebook":
            siteURL = "http://www.facebook.com/share.php?u=" + encodeURIComponent(link) + "&t=" + encodeURIComponent(text);
        break;
        case "twitter":
            siteURL = "http://twitter.com/share?text=" + encodeURIComponent(text) + "&url=" + encodeURIComponent(link);
        break;
    }
   
    // open new popup window to site
    var urlRequest:URLRequest = new URLRequest(siteURL);
    navigateToURL(urlRequest, "_blank");
}

Truncating the tweet text and ending it with an ellipsis was something I saw on Sascha Kimmel's blog.

When creating a Facebook post for a web page, you can specify the page's title, description and thumbnail that appear on the Facebook post. This is optional, as Facebook will try to find a good thumbnail image and text for the page if they are not provided by you. To provide them, add the following meta tags to the HEAD section of the web page:

<meta property="og:title" content="Views from Space" />
<meta property="og:description" content="Photos of Earth from the International Space Station's Cupola" />
<meta property="og:image" content="http://www.esa.int/images/iss025e009840,0.jpg" />

The image for the thumbnail should be roughly square, with minimum dimensions of 50 x 50 pixels. It can be a PNG, JPEG, or GIF image.


Spread The Word


3 Responses to "Facebook and Twitter buttons in Flash and Flex"

 
loic
said this on 04 Apr 2011 4:10:28 AM CDT
Hi,

That's a great stuff. I would like to use it in my project (maybe a paid-for). How can I contact you to tell you more ?

Loic

 
Mohanan
said this on 10 Feb 2012 10:22:13 PM CDT
to help. I&#8217;ve been saved many time by ohetrs who blogged about the same issue I was having. It feels great to return the favor.

 
Otkup Zlata
said this on 24 Mar 2012 3:06:43 AM CDT
Something is wrong, cant get like button to work (show number of likes) an so on. Damn!




Leave a reply:
Your Name *: Email (private) *: Website:
Please copy the characters from the image below into the text field below. Doing this helps us prevent automated submissions.
Security Code: img

Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.