<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rommel Santor&#039;s Clog &#187; dynamic</title>
	<atom:link href="https://rommelsantor.com/clog/tag/dynamic/feed/" rel="self" type="application/rss+xml" />
	<link>https://rommelsantor.com/clog</link>
	<description>my exploits and misadventures in programming</description>
	<lastBuildDate>Thu, 04 Feb 2016 12:56:01 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>Dynamic Drop-Down Navigation Menu Like Wired.com&#8217;s</title>
		<link>https://rommelsantor.com/clog/2010/04/24/dynamic-drop-down-navigation-menu-like-wired-com/</link>
		<comments>https://rommelsantor.com/clog/2010/04/24/dynamic-drop-down-navigation-menu-like-wired-com/#comments</comments>
		<pubDate>Sun, 25 Apr 2010 04:14:42 +0000</pubDate>
		<dc:creator><![CDATA[rommel]]></dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[drop down]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[menu]]></category>
		<category><![CDATA[navigation]]></category>
		<category><![CDATA[pop open]]></category>

		<guid isPermaLink="false">http://rommelsantor.com/clog/?p=89</guid>
		<description><![CDATA[A friend recently asked me for a tip on how to create a dynamic pop-up menu like the one used on the main navigation bar of Wired.com. It&#8217;s a pretty typical implementation of a menu that pops open when the user hovers over a navigation tab, but I thought I&#8217;d share the solution I came [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>A friend recently asked me for a tip on how to create a dynamic pop-up menu like the one used on the main navigation bar of Wired.com. It&#8217;s a pretty typical implementation of a menu that pops open when the user hovers over a navigation tab, but I thought I&#8217;d share the solution I came up with.</p>
<p>This is a screenshot of what the menu currently looks like at Wired.com:<br />
<img class="aligncenter size-full wp-image-92" title="Wired.com Navigation Menu" src="http://rommelsantor.com/clog/wp-content/uploads/2010/04/wired-menu.png" alt="" width="654" height="234" /><br />
<span id="more-89"></span><br />
And this is a screenshot of the menu I created:<br />
<img class="aligncenter size-full wp-image-93" title="My Menu Like Wired.com's" src="http://rommelsantor.com/clog/wp-content/uploads/2010/04/my-wired-menu.png" alt="" width="436" height="168" /></p>
<p>I just used a bit of straightforward CSS and jQuery JavaScript to get it done. Below is the code and a functional demo for your viewing pleasure. Admittedly it&#8217;s not the most elegant solution, but it suffices and provides the bare essentials that can be tweaked to suit your liking.</p>
<h2>CSS</h2>

<pre class="css:nocontrols:nogutter">
#navbar {
  width: 100%;
  height: 25px;
  background: #000;
}

#navbar &gt; ul {
  list-style: none;
}

#navbar &gt; ul &gt; li {
  float: left;
  display: inline-block;
}

#navbar &gt; ul &gt; li &gt; a {
  display: block;
  color: #fff;
  padding: 5px 20px;
  text-align: center;
}

#navbar &gt; ul &gt; li &gt; a:hover, #navbar &gt; ul &gt; li &gt; a.open {
  background: #888;
}

#navbar .dropdown {
  display: none;
  position: absolute;
  margin-top: -2px;
  background: #fafafa;
  border: 3px solid #aaa;
  padding: 5px;
  padding-left: 0;
}

#navbar .dropdown ul {
  list-style-type: none;
  display: inline-block;
  float: left;
  margin: 0 0 0 5px;
  padding: 0;
}

#navbar .dropdown ul li {
  border-bottom: 1px solid #aaa;
  margin: 0;
  padding: 0;
}

/* would use :last-child but IE doesn't recognize it */
#navbar .dropdown ul li.last {
  border: 0;
}

#navbar .dropdown ul li a {
  display: block;
  min-width: 100px;
  padding: 3px 5px 3px 10px;
  margin: 1px 0;
  font-weight: normal;
  font-size: 0.85em;
}

#navbar .dropdown ul li a:hover {
  background: #eee;
}
</pre>

<h2>HTML</h2>

<pre class="html:nocontrols:nogutter">
&lt;div id="navbar"&gt; 
  &lt;ul&gt; 
    &lt;li&gt; 
      &lt;a href="#"&gt;Apple&lt;/a&gt; 
      &lt;div class="dropdown"&gt; 
        &lt;ul&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column A #1&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column A #2&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column A #3&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column A #4&lt;/a&gt;&lt;/li&gt; 
          &lt;li class="last"&gt;&lt;a href="#"&gt;Column A #5&lt;/a&gt;&lt;/li&gt; 
        &lt;/ul&gt; 
        &lt;ul&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column B #1&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column B #2&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column B #3&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column B #4&lt;/a&gt;&lt;/li&gt; 
          &lt;li class="last"&gt;&lt;a href="#"&gt;Column B #5&lt;/a&gt;&lt;/li&gt; 
        &lt;/ul&gt; 
      &lt;/div&gt; 
    &lt;/li&gt; 
    &lt;li&gt; 
      &lt;a href="#"&gt;Banana&lt;/a&gt; 
      &lt;div class="dropdown"&gt; 
        &lt;ul&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column A #1&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column A #2&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column A #3&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column A #4&lt;/a&gt;&lt;/li&gt; 
          &lt;li class="last"&gt;&lt;a href="#"&gt;Column A #5&lt;/a&gt;&lt;/li&gt; 
        &lt;/ul&gt; 
        &lt;ul&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column B #1&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column B #2&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column B #3&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column B #4&lt;/a&gt;&lt;/li&gt; 
          &lt;li class="last"&gt;&lt;a href="#"&gt;Column B #5&lt;/a&gt;&lt;/li&gt; 
        &lt;/ul&gt; 
      &lt;/div&gt; 
    &lt;/li&gt; 
    &lt;li&gt; 
      &lt;a href="#"&gt;Cherry&lt;/a&gt; 
      &lt;div class="dropdown"&gt; 
        &lt;ul&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column A #1&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column A #2&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column A #3&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column A #4&lt;/a&gt;&lt;/li&gt; 
          &lt;li class="last"&gt;&lt;a href="#"&gt;Column A #5&lt;/a&gt;&lt;/li&gt; 
        &lt;/ul&gt; 
        &lt;ul&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column B #1&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column B #2&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column B #3&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column B #4&lt;/a&gt;&lt;/li&gt; 
          &lt;li class="last"&gt;&lt;a href="#"&gt;Column B #5&lt;/a&gt;&lt;/li&gt; 
        &lt;/ul&gt; 
      &lt;/div&gt; 
    &lt;/li&gt; 
    &lt;li&gt; 
      &lt;a href="#"&gt;Durian&lt;/a&gt; 
      &lt;div class="dropdown"&gt; 
        &lt;ul&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column A #1&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column A #2&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column A #3&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column A #4&lt;/a&gt;&lt;/li&gt; 
          &lt;li class="last"&gt;&lt;a href="#"&gt;Column A #5&lt;/a&gt;&lt;/li&gt; 
        &lt;/ul&gt; 
        &lt;ul&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column B #1&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column B #2&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column B #3&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column B #4&lt;/a&gt;&lt;/li&gt; 
          &lt;li class="last"&gt;&lt;a href="#"&gt;Column B #5&lt;/a&gt;&lt;/li&gt; 
        &lt;/ul&gt; 
      &lt;/div&gt; 
    &lt;/li&gt; 
    &lt;li&gt; 
      &lt;a href="#"&gt;Elephant&lt;/a&gt; 
      &lt;div class="dropdown"&gt; 
        &lt;ul&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column A #1&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column A #2&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column A #3&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column A #4&lt;/a&gt;&lt;/li&gt; 
          &lt;li class="last"&gt;&lt;a href="#"&gt;Column A #5&lt;/a&gt;&lt;/li&gt; 
        &lt;/ul&gt; 
        &lt;ul&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column B #1&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column B #2&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column B #3&lt;/a&gt;&lt;/li&gt; 
          &lt;li&gt;&lt;a href="#"&gt;Column B #4&lt;/a&gt;&lt;/li&gt; 
          &lt;li class="last"&gt;&lt;a href="#"&gt;Column B #5&lt;/a&gt;&lt;/li&gt; 
        &lt;/ul&gt; 
      &lt;/div&gt; 
    &lt;/li&gt; 
  &lt;/ul&gt; 
&lt;/div&gt;
</pre>

<h2>jQuery JavaScript</h2>

<pre class="js:nocontrols:nogutter">
$("#navbar &gt; ul &gt; li &gt; a").mouseover(function(){
  $("#navbar &gt; ul &gt; li &gt; a").each(function(){
    $(this).removeClass('over open');
    $(this).next().removeClass('over').hide();
  });

  $(this).addClass('over open');
  $(this).next().show();
});
$("#navbar &gt; ul &gt; li &gt; a").mouseout(function(){
  var a = $(this);
  a.removeClass('over');
  setTimeout(function(){
    if (a.hasClass('over') || a.next().hasClass('over')) return;
    a.removeClass('open');
    a.next().hide();
    }, 1000);
});
$("#navbar &gt; ul &gt; li &gt; div.dropdown").mouseover(function(){
  $(this).addClass('over');
});
$("#navbar &gt; ul &gt; li &gt; div.dropdown").mouseout(function(){
  var a = $(this);
  a.removeClass('over');
  setTimeout(function(){
    if (a.hasClass('over') || a.prev().hasClass('over')) return;
    a.prev().removeClass('open');
    a.hide();
    }, 1000);
});
</pre>


<div style="float:left;margin:0 10px 10px 0">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-7639656561235411";
/* Square (250x250) */
google_ad_slot = "8522038794";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<div style="clear:left"></div>

<h1>Demonstration</h1>

<style type="text/css">
#navbar {
width: 100%;
height: 25px;
background: #000;
}

#navbar > ul {
list-style: none;
}

#navbar > ul > li {
float: left;
display: inline-block;
}

#navbar > ul > li > a {
display: block;
color: #fff;
padding: 5px 20px;
text-align: center;
}

#navbar > ul > li > a:hover, #navbar > ul > li > a.open {
background: #888;
}

#navbar .dropdown {
display: none;
position: absolute;
margin-top: -2px;
background: #fafafa;
border: 3px solid #aaa;
padding: 5px;
padding-left: 0;
}

#navbar .dropdown ul {
list-style-type: none;
display: inline-block;
float: left;
margin: 0 0 0 5px;
padding: 0;
}

#navbar .dropdown ul li {
border-bottom: 1px solid #aaa;
margin: 0;
padding: 0;
}

#navbar .dropdown ul li.last {
border: 0;
}

#navbar .dropdown ul li a {
display: block;
min-width: 100px;
padding: 3px 5px 3px 10px;
margin: 1px 0;
font-weight: normal;
font-size: 0.85em;
}

#navbar .dropdown ul li a:hover {
background: #eee;
}
</style>
<div id="navbar">
<ul>
	<li> <a href="#">Apple</a>
<div class="dropdown">
<ul>
	<li><a href="#">Column A #1</a></li>
	<li><a href="#">Column A #2</a></li>
	<li><a href="#">Column A #3</a></li>
	<li><a href="#">Column A #4</a></li>
	<li class="last"><a href="#">Column A #5</a></li>
</ul>
<ul>
	<li><a href="#">Column B #1</a></li>
	<li><a href="#">Column B #2</a></li>
	<li><a href="#">Column B #3</a></li>
	<li><a href="#">Column B #4</a></li>
	<li class="last"><a href="#">Column B #5</a></li>
</ul>
</div></li>
	<li> <a href="#">Banana</a>
<div class="dropdown">
<ul>
	<li><a href="#">Column A #1</a></li>
	<li><a href="#">Column A #2</a></li>
	<li><a href="#">Column A #3</a></li>
	<li><a href="#">Column A #4</a></li>
	<li class="last"><a href="#">Column A #5</a></li>
</ul>
<ul>
	<li><a href="#">Column B #1</a></li>
	<li><a href="#">Column B #2</a></li>
	<li><a href="#">Column B #3</a></li>
	<li><a href="#">Column B #4</a></li>
	<li class="last"><a href="#">Column B #5</a></li>
</ul>
</div></li>
	<li> <a href="#">Cherry</a>
<div class="dropdown">
<ul>
	<li><a href="#">Column A #1</a></li>
	<li><a href="#">Column A #2</a></li>
	<li><a href="#">Column A #3</a></li>
	<li><a href="#">Column A #4</a></li>
	<li class="last"><a href="#">Column A #5</a></li>
</ul>
<ul>
	<li><a href="#">Column B #1</a></li>
	<li><a href="#">Column B #2</a></li>
	<li><a href="#">Column B #3</a></li>
	<li><a href="#">Column B #4</a></li>
	<li class="last"><a href="#">Column B #5</a></li>
</ul>
</div></li>
	<li> <a href="#">Durian</a>
<div class="dropdown">
<ul>
	<li><a href="#">Column A #1 with a really long label</a></li>
	<li><a href="#">Column A #2</a></li>
	<li><a href="#">Column A #3</a></li>
	<li><a href="#">Column A #4</a></li>
	<li><a href="#">Column A #5</a></li>
	<li><a href="#">Column A #6</a></li>
	<li><a href="#">Column A #7</a></li>
	<li><a href="#">Column A #8</a></li>
	<li><a href="#">Column A #9</a></li>
	<li class="last"><a href="#">Column A #10</a></li>
</ul>
</div></li>
	<li> <a href="#">Elephant</a>
<div class="dropdown">
<ul>
	<li><a href="#">Column A #1</a></li>
	<li><a href="#">Column A #2</a></li>
	<li><a href="#">Column A #3</a></li>
	<li><a href="#">Column A #4</a></li>
	<li class="last"><a href="#">Column A #5</a></li>
</ul>
<ul>
	<li><a href="#">Column B #1</a></li>
	<li><a href="#">Column B #2</a></li>
	<li><a href="#">Column B #3</a></li>
	<li><a href="#">Column B #4</a></li>
	<li class="last"><a href="#">Column B #5</a></li>
</ul>
</div></li>
</ul>
</div>
<script type="text/javascript">
$("pre").attr({name:'code'});

$("#navbar > ul > li > a").mouseover(function(){
  $("#navbar > ul > li > a").each(function(){
    $(this).removeClass('over open');
    $(this).next().removeClass('over').hide();
  });

  $(this).addClass('over open');
  $(this).next().show();
});
$("#navbar > ul > li > a").mouseout(function(){
  var a = $(this);
  a.removeClass('over');
  setTimeout(function(){
    if (a.hasClass('over') || a.next().hasClass('over')) return;
    a.removeClass('open');
    a.next().hide();
    }, 1000);
});
$("#navbar > ul > li > div.dropdown").mouseover(function(){
  $(this).addClass('over');
});
$("#navbar > ul > li > div.dropdown").mouseout(function(){
  var a = $(this);
  a.removeClass('over');
  setTimeout(function(){
    if (a.hasClass('over') || a.prev().hasClass('over')) return;
    a.prev().removeClass('open');
    a.hide();
    }, 1000);
});
</script>

]]></content:encoded>
			<wfw:commentRss>https://rommelsantor.com/clog/2010/04/24/dynamic-drop-down-navigation-menu-like-wired-com/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Dynamic Facebook and Tweetmeme Widgets</title>
		<link>https://rommelsantor.com/clog/2010/04/09/dynamic-facebook-and-tweetmeme-widgets/</link>
		<comments>https://rommelsantor.com/clog/2010/04/09/dynamic-facebook-and-tweetmeme-widgets/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 21:19:27 +0000</pubDate>
		<dc:creator><![CDATA[rommel]]></dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[VideoSift]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[FB.Share]]></category>
		<category><![CDATA[retweet]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[tweetmeme]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[widget]]></category>

		<guid isPermaLink="false">http://rommelsantor.com/clog/?p=45</guid>
		<description><![CDATA[We just made a change on YouTube videos so that at the end of playback a Facebook &#8220;share&#8221; button and a Tweetmeme &#8220;retweet&#8221; button appear as part of an effort to encourage users to spread videos in their personal social networks. This presented a couple of problems because, similar to an issue touched on in [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>
We just made a change on YouTube videos so that at the end of playback a Facebook &#8220;share&#8221; button and a Tweetmeme &#8220;retweet&#8221; button appear as part of an effort to encourage users to spread videos in their personal social networks.
</p>
<p><a rel="attachment wp-att-46" href="http://rommelsantor.com/clog/2010/04/09/dynamic-facebook-and-tweetmeme-widgets/shares/"><img class="aligncenter size-full wp-image-46" style="border: 1px solid #999;" title="Dynamic Facebook and Retweet Widgets" src="http://rommelsantor.com/clog/wp-content/uploads/2010/04/shares.jpg" alt="" width="537" height="407" /></a><br />
<span id="more-45"></span></p>
<p>
This presented a couple of problems because, similar to an issue touched on in <a href="http://rommelsantor.com/clog/2010/04/06/javascript-video-embed-widget/">my last clog</a>, the widgets needed to be inserted dynamically and that&#8217;s usually easier said than done. After a little bit of digging into how the widgets function, I was able to make them seemingly load normally on-the-fly.
</p>
<h1>Dynamic Facebook Share Button</h1>
<p>
Facebook share buttons are created in a couple of pieces. I started with the default widget code <a href="http://www.facebook.com/facebook-widgets/share.php" target="_blank">supplied</a> by Facebook:
</p>
<pre class="html:nocontrols:nogutter">
&lt;a type="button_count" name="fb_share"
  href="http://www.facebook.com/sharer.php"&gt;Share&lt;/a&gt;
&lt;script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"&gt;&lt;/script&gt;
</pre>
<p>
Understanding how to make this work dynamically first means understanding what exactly this code does. The &lt;a&gt; tag is clearly a link to Facebook&#8217;s share service where users can submit our page. The <tt>&lt;script&gt;</tt> tag loads a JavaScript object named FB.Share.
</p>
<h2>Failed Insertion</h2>
<p>If you were to try inserting these bits of code dynamically, you&#8217;d discover in no time flat that it just won&#8217;t work. For example, this might look like a good idea, but it&#8217;s useless:</p>
<pre>
$("#somediv").append('&lt;a type="button_count" name="fb_share" '+
  'href="http://www.facebook.com/sharer.php"&gt;Share&lt;/a&gt;');
$("#somediv").append('&lt;script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" '+
  'type="text/javascript"&gt;&lt;/script&gt;');
</pre>
<p>
And you may even try it a little more manually by directly modifying the DOM:
</p>
<pre>a = document.createElement('a');
a.type='button_count';
a.name='fb_share';
a.href='http://www.facebook.com/sharer.php';
a.innerHTML = 'Share';
document.getElementById('somediv').appendChild(a);

s = document.createElement('script');
s.type='text/javascript';
s.src='http://static.ak.fbcdn.net/connect.php/js/FB.Share';
document.getElementById('somediv').appendChild(s);
</pre>
<p>
But you&#8217;ll be disappointed by the results. A working solution required a little more digging.
</p>
<h2>Successful Buttonization</h2>
<p>I opened <a href="http://static.ak.fbcdn.net/connect.php/js/FB.Share" target="_blank">Facebook&#8217;s JavaScript file</a> and started sniffing around. Their code is designed to initialize all buttons on the page just once, which makes perfect sense. So the solution is to add the <tt>&lt;a&gt;</tt> tag as per the above but then manually force FB.Share to re-execute, thus turning our dynamically added link into a button. The required function call is rather simple:
</p>
<pre>
if (typeof(FB.Share) != 'undefined') {// make sure it exists
  // FB.Share.onFirst();
  // *** UPDATE: 17 Aug 2011 - Thanks to zr0ck who informed
  // FB has modified their Share script ***
  // FB.Share.onFirst() no longer exists; you must now instead call:
  FB.Share.renderPass();
}
</pre>
<p>
Just execute that after you&#8217;ve inserted your <tt>&lt;a&gt;</tt> link and it&#8217;ll become a pretty Facebook button.
</p>

<div style="float:left;margin:0 10px 10px 0">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-7639656561235411";
/* Square (250x250) */
google_ad_slot = "8522038794";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<h2>Multiple Buttons Per Page</h2>
<p>The above solution works splendidly, assuming that you use it on a URL specific to the content you&#8217;re sharing because FB.Share will by default use the address of the current page when adding to the user&#8217;s wall. However, if you are on a listing page where there are multiple content posts and the URL is not specific to any one of them (e.g., http://videosift.com), then every dynamic button you add will reference the same address.
</p>
<p>
Facebook provides a straightforward method for you to specify an alternate URL than the current one, which is to add a <tt>u</tt> variable to the query string of the link:
</p>
<pre class="html:nocontrols:nogutter">
&lt;a type="button_count" name="fb_share"
  href="http://www.facebook.com/sharer.php?u=SOMEURL"&gt;
</pre>
<p>
However that method doesn&#8217;t work with our dynamic buttonizing for whatever reason. A little more sniffing through the FB.Share code reveals exactly how it determines what URL to use:
</p>
<pre>
FB.Share {
//...
getUrl:function(a){return a.getAttribute('share_url')||window.location.href;}
//...
}
</pre>
<p>
Aha- it checks for an attribute named <tt>share_url</tt>. So all we have to do is add that attribute to our link et voila, we&#8217;re in business!
</p>
<pre class="html:nocontrols:nogutter">
&lt;a type="button_count" name="fb_share"
  href="http://www.facebook.com/sharer.php" share_url="SOMEURL"&gt;
</pre>
<h1>Dynamic Tweetmeme Retweet Button</h1>
<p>
Fortunately, the retweet button was much less involved than the FB share button, but also require just a little bit of digging. As described on <a href="http://help.tweetmeme.com/2009/04/06/tweetmeme-button/" target="_blank">tweetmeme&#8217;s widget page</a>, the following is the code you&#8217;d typically want to use on a web page:
</p>
<pre>
&lt;script type="text/javascript"&gt;
tweetmeme_url = 'http://yoururl.com';
&lt;/script&gt;
&lt;script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"&gt;&lt;/script&gt;
</pre>
<p>
As with the FB widget, attempting to insert this code into the DOM on-the-fly just results in failure. (I&#8217;ll spare you the sample broken code.) So I cracked open the <a href="http://tweetmeme.com/i/scripts/button.js" target="_blank">button&#8217;s js code</a> and investigated how it worked.
</p>
<p>
It turns out tweetmeme doesn&#8217;t use a lot of JS tomfoolery to make their widgets work. Instead, they insert a simple <tt>&lt;iframe&gt;</tt> and allow that to do all work, which makes it easy for us because all we have to do is insert the same type of iframe and we&#8217;re in business. So we did something like the following and it worked like a charm:
</p>
<pre>
$("#somediv").append('&lt;iframe frameborder="0" scrolling="no" height="61" width="50"
  src="http://api.tweetmeme.com/button.js?url='+SOMEURL+'"&gt;&lt;/iframe&gt;');
</pre>
<h1>Enjoy!</h1>
<p><script type="text/javascript">//<![CDATA[
$("pre").attr({name:'code'}).each(function(){if($(this).attr('class')=='')$(this).addClass('js:nocontrols:nogutter');});
//]]&gt;</script></p>
]]></content:encoded>
			<wfw:commentRss>https://rommelsantor.com/clog/2010/04/09/dynamic-facebook-and-tweetmeme-widgets/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
