Build Internet has a brand new theme, and that's only the beginning. Read the full story or hide this bar

Updating Social Widgets with the History API

Keep social widgets in sync with pages loaded via history API

Keep Your Social Current

After working with the HTML5 history API for the launch of You Rather, I hit a snag with keeping social widgets current. The content and URL would update, but the Like button would stick to the original document location. As it turns out, Twitter and Facebook widgets that can’t simply be updated via typical jQuery selectors since they are parsed into iframes once the page loads. Fortunately for us, there is an easy fix.

In the function below, you can see that on push state we replace the HTML of the widget’s parent with the new unparsed snippet. Then we search for a window level object of FB for Facebook and twttr for Twitter and reload each if they exist. For those looking to update Google Analytics you’ll also notice there is a section where the URL is pushed as a new pageview. You earned those pageviews, so don’t forget to claim them.

var update_social = function(title, url) {
	
	// Add a new pageview to Analytics
	if (typeof _gaq !== 'undefined')
	{
		window._gaq.push(['_trackPageview', url]);
	}
	
	//Need the full URL for outside sites
	var full_url = window.location.href;
	
	//Populate new Facebook widget
	$('#fb-like').html('<fb:like href="' + full_url + '" show_faces="false" width="350" action="like" send="true" font="segoe ui" colorscheme="light" />');
	
	//Reload Facebook
	if (typeof FB !== 'undefined') {
		FB.XFBML.parse(document.getElementById('fb-like'));
	}
	
	//Populate new Twitter widget
	var twitter_text = title + " via " + "@buildinternet";
	$('#twitter-share').html('<a href="https://twitter.com/share" vclass="twitter-share-button" data-url="' + full_url + '" data-text="'+ twitter_text +'" data-related="buildinternet">Tweet</a>');
	
	//Reload Twitter
	if (typeof twttr !== 'undefined') {
		twttr.widgets.load();
	}
}

For a look at this in action, take a look at the the demo page. The source code shows more of the context in a working History API driven page.

If you’re looking for more reading on how to get started with the history API, I recommend taking a look at this tutorial before looping back.

Wordpress.com stats not installed! Posted Friday, March 2nd, 2012 / Back to Top

I this post. Tweet
SPONSOR

35 Comments 0 Mentions

  1. شات صوتي Author Editor

    You are great and thank for you articles

    March 6, 2012 · Reply

    • kodak ink cartridges Author Editor

      My remarks are also like you about him.

      January 29, 2013 · Reply

      • lexmark ink cartridges Author Editor

        I am agree with your opinion but this is wise to consult some one which have experience.

        February 14, 2013 ·

  2. طراحی سایت Author Editor

    The demo is working but I can’t get the snippet of code to work. I’m doing something wrong.

    April 10, 2012 · Reply

  3. Bharat Chowdary Author Editor

    thanks for the demo and download link.

    April 19, 2012 · Reply

  4. Sarah Author Editor

    Thank you for sharing the codes and information with your readers here. I’ll work with them and let you know the feedback. Sicily Food & Wine Tours

    May 6, 2012 · Reply

  5. bvjf89safka Author Editor

    Thank you for sharing the codes and information with your readers here. I’ll work with them and let you know the feedback. Sicily Food & Wine Tours

    May 20, 2012 · Reply

  6. Caley Jack Author Editor

    I am trying to do something very similar with the History API. However, instead of using the Like button for Facebook, I am using FB.ui to create a dialog box using the feed method.

    The problem I am running into is that I am getting multiple dialog boxes. The more I click through my website, the more boxes get created. They tend to stack up on each other and for whatever reason the layering order keeps changing so it looks like a slideshow of all the loaded dialog boxes.

    Is it possible to reset/remove the iframes that are created in #fb-root? I have tried using jQuery’s remove() and empty() but that just causes errors. Any thoughts would be appreciated.

    May 21, 2012 · Reply

    • phphunger Author Editor

      Hi Caley Jack, could you please suggest me how to work out on this for a blogger blog…actually i didn’t understand properly..as i am novice in this FB kind of stuff…Thanks in advance…

      June 22, 2012 · Reply

  7. Arun Kumar J Author Editor

    Thanks for demo and the codes.

    June 20, 2012 · Reply

  8. phphunger Author Editor

    nice work but can anyone suggest me where can i apply this for blogger..is there any custom widget available for this…

    June 22, 2012 · Reply

  9. Christian Louboutin Author Editor

    Don’t forget the things you once you owned. Treasure the things you can’t get. Don’t give up the things that belong to you and keep those lost things in memory.  

    June 28, 2012 · Reply

  10. saha Author Editor

    I agree with you. Updating social widgets is important. Thanks for sharing this code snippet for help.

    July 19, 2012 · Reply

  11. John Bash Author Editor

    This is damn cool, thanks for sharing the snippet. Will try my hands on this in my free time.

    July 26, 2012 · Reply

  12. زعفران Author Editor

    Works like a charm here.
    It’s great. I appreciate it.

    August 16, 2012 · Reply

  13. http://researchpaperstar.net/ Author Editor

    So nice grest so nic gret!

    September 28, 2012 · Reply

  14. DesignSanduiche.com Author Editor

    Great article and tutorial. Thanks!

    October 8, 2012 · Reply

  15. طراحی سایت Author Editor

    The demo is working but I can’t get the snippet of code to work. I’m doing something wrong.

    November 10, 2012 · Reply

  16. kodak ink cartridges Author Editor

    This is very very imaginative and creative approach, nice buddy,

    January 29, 2013 · Reply

  17. http://essaystar.org/ Author Editor

    Oh well done very nice great!

    February 11, 2013 · Reply

  18. sonu sindhu Author Editor

    Very nice such a helpful
    Thanks

    February 17, 2013 · Reply

  19. Jan Author Editor

    Very nice article and very helpful for me! Thanks so much.

    February 26, 2013 · Reply

  20. سئو Author Editor

    i like it tnx

    February 26, 2013 · Reply

  21. david Author Editor

    I found this is an educational and interesting publication, so I think it is very useful and knowledgeable. Thank you for the efforts you have made in writing this submission. I am hoping the analogous best work from you next time as well. practically your creative writing ability has inspired me.

    March 4, 2013 · Reply

  22. درب اتوماتیک Author Editor

    very good :)))))))))))))))

    March 7, 2013 · Reply

  23. mahesh Author Editor

    I am running into is that I am getting multiple dialog boxes. The more I click through my website, the more boxes get created. They tend to stack up on each other and for whatever reason the layering order keeps changing so it looks like a slideshow of all the loaded dialog boxes.thanks

    March 9, 2013 · Reply

  24. Alex Author Editor

    Brilliant and simple – thank you! :) Social media integration is the bane of my existence, but alas it seems a necessary evil in this day and age.

    March 14, 2013 · Reply

  25. http://essayscope Author Editor

    Thank for an informative post. I’m working like an business analyst, so it was useful.

    April 4, 2013 · Reply

  26. Sahar Author Editor

    Thanks for demo and the CODES!

    April 10, 2013 · Reply

  27. davetiye Author Editor

    I tried it for protect my history.

    April 13, 2013 · Reply

  28. Jay Author Editor

    thanks for this post! it’s very useful.

    April 22, 2013 · Reply

  29. Paul D. Mitchell @ business web design company Author Editor

    By utilizing social media widgets and making your job more ocular on general avenues on the net can graphite to increment in your business’ popularity, if you witticism your cards good.

    April 28, 2013 · Reply

  30. web design naperville il Author Editor

    Thanks for sharing this. This will prove useful.

    May 7, 2013 · Reply

  31. Sharath Author Editor

    Excellent Post. helped a lot

    May 17, 2013 · Reply

  32. pro-writing-essay.com Author Editor

    This article seems to me very interesting!!! Thanks a lot for your nice work!

    16 hours ago · Reply

 

Join the Conversation

Back to Top / Comment RSS

2012 Build Internet. Created by One Mighty Roar. Icons by Komodo Media. Back to Top