<?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>Tech Mug &#187; Javascript</title>
	<atom:link href="http://www.techmug.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.techmug.com</link>
	<description></description>
	<lastBuildDate>Tue, 31 Jan 2012 12:15:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>AJAX Currency Converter with Google API</title>
		<link>http://www.techmug.com/ajax-currency-converter-with-google-api/</link>
		<comments>http://www.techmug.com/ajax-currency-converter-with-google-api/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 15:42:03 +0000</pubDate>
		<dc:creator>Fraz Ahmed</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Converter]]></category>
		<category><![CDATA[Currency]]></category>
		<category><![CDATA[development]]></category>

		<guid isPermaLink="false">http://www.techmug.com/?p=211</guid>
		<description><![CDATA[No doubt, Google is a magical search engine. Google also provides many APIs to be incorporated in your application. Today, we will use Google&#8217;s hidden currency conversion API. Why hidden? Because there is no official documentation for it. We will build a currency converter from scratch, it pulls data from Google and display the results [...]]]></description>
			<content:encoded><![CDATA[<p>No doubt, Google is a magical search engine. Google also provides many APIs to be incorporated in your application. Today, we will use Google&#8217;s hidden currency conversion API. Why hidden? Because there is no official documentation for it.</p>
<p><span id="more-211"></span></p>
<p>We will build a currency converter from scratch, it pulls data from Google and display the results using jQuery AJAX function. For you better understanding, you may visit demo or download source code below:</p>
<p style="text-align: center;"><strong><a href="/demo/currency-converter/techmug-currency-converter.zip"><img class="alignnone size-full wp-image-337" title="Download Source Code" src="http://www.techmug.com/wp-content/uploads/2010/10/btn-source-code.jpg" alt="Download Source Code" width="140" height="90" /></a><a href="/demo/currency-converter/" target="_blank"><img class="alignnone size-full wp-image-336" title="See Demo" src="http://www.techmug.com/wp-content/uploads/2010/10/btn-see-demo.jpg" alt="See Demo" width="140" height="90" /></a></strong></p>
<p>Basically our whole code consist of three components &#8211; HTML for displaying interface, Javascript for AJAX functionality and PHP for fetching and manipulating conversion results from Google.</p>
<p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-7217075623453662";
google_ad_slot = "2820701010";
google_ad_width = 336;
google_ad_height = 280;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<center><font-size="9px">Advertisement</font></center></p>
<h1>HTML Structure</h1>
<p>HTML structure for this application is simple enough. There are two drop downs, one input field, one button and a div for displaying results. Drop downs are used for selecting From Currency and To Currency while input field is used for entering the desired amount to be converted in your selected currency. Conversion result displays in results div once you click on convert button. When you click on the button, it fires a Javascript function which is described below.</p>
<h1>Javascript</h1>
<p>Following is the code for our AJAX enabled javascript. Below code is executed upon clicking on convert button.</p>
<pre class="brush: jscript; title: ; notranslate">

$(document).ready(function() {

 $('#convert').click(function(){

 //Get all the values
 var amount = $('#amount').val();
 var from = $('#fromCurrency').val();
 var to = $('#toCurrency').val();

 //Make data string
 var dataString = &quot;amount=&quot; + amount + &quot;&amp;from=&quot; + from + &quot;&amp;to=&quot; + to;

 $.ajax({
 type: &quot;POST&quot;,
 url: &quot;ajax_converter.php&quot;,
 data: dataString,

 success: function(data){
 //Show results div
 $('#results').show();

 //Put received response into result div
 $('#results').html(data);
 }

 });
 });
});
</pre>
<p>Above code is very simple and should be self explanatory, if you are familiar with jQuery AJAX function. For those who does not know, what&#8217;s going on here I tr to explain them.</p>
<p><em>$(&#8216;#convert&#8217;).click(function&#8230;</em> this is the starting point of our code, it means that execute the underneath code of this function when an element with id of  &#8216;convert&#8217; is clicked. In our case this id is used for &#8216;convert&#8217; button.</p>
<p>After that we grab values from our HTML elements by using <em>.val()</em> function of jQuery and assign these values into Javascript variables. These variables are then used in dataString variable.</p>
<p>In our AJAX function we are using POST method and in &#8216;url&#8217;, we provide the url of our PHP file which will provide conversion results back to this AJAX function. We are sending &#8216;dataString&#8217; as data to our PHP script.</p>
<p>After successful iterating to our PHP script, this AJAX function first shows our results div and out the received data into our results div as HTML.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-7217075623453662";
google_ad_slot = "4831550702";
google_ad_width = 300;
google_ad_height = 250;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<center><font-size="9px">Advertisement</font></center></p>
<h1>PHP Code</h1>
<p>The brain of our application is below PHP code.</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
//Get Posted data
$amount = $_POST['amount'];
$from = $_POST['from'];
$to = $_POST['to'];

//make string to be put in API
$string = &quot;1&quot;.$from.&quot;=?&quot;.$to;

//Call Google API
$google_url = &quot;http://www.google.com/ig/calculator?hl=en&amp;q=&quot;.$string;

//Get and Store API results into a variable
$result = file_get_contents($google_url);

//Explode result to convert into an array
$result = explode('&quot;', $result);

################################
# Right Hand Side
################################
$converted_amount = explode(' ', $result[3]);
$conversion = $converted_amount[0];
$conversion = preg_replace('/[\x00-\x08\x0B-\x1F]/', '', $conversion);
$conversion = $conversion * $amount;
$conversion = round($conversion, 2);

//Get text for converted currency
$rhs_text = ucwords(str_replace($converted_amount[0],&quot;&quot;,$result[3]));

//Make right hand side string
$rhs = $conversion.$rhs_text;

################################
# Left Hand Side
################################
$google_lhs = explode(' ', $result[1]);
$from_amount = $google_lhs[0];

//Get text for converted from currency
$from_text = ucwords(str_replace($from_amount,&quot;&quot;,$result[1]));

//Make left hand side string
$lhs = $amount.&quot; &quot;.$from_text;

################################
# Make the result
################################

echo $lhs.&quot; = &quot;.$rhs;
?&gt;
</pre>
<p>Above code is well commented to make you understand better. In this code we are using some built-in PHP functions &#8211; <a href="http://php.net/manual/en/function.file-get-contents.php" target="_blank">file_get_contents</a>, <a href="http://php.net/manual/en/function.explode.php" target="_blank">explode</a>, <a href="http://php.net/manual/en/function.round.php" target="_blank">round</a> and <a href="http://php.net/manual/en/function.ucwords.php">ucwords</a>.</p>
<ul>
<li>In start we grab the values posted to this script by our AJAX function. We store these values into variables.</li>
<li>We make a string variable using above explained variables.</li>
<li>We concatenate this string variable to Google Currency Calculator API url and store the url in a new variable named $google_url.</li>
<li>We fetch the data of $google_url to $result variable using PHP&#8217;s <a href="http://php.net/manual/en/function.file-get-contents.php" target="_blank">file_get_contents</a> function.</li>
<li>We receive data from Google something similar to<br />
<em><strong>{lhs: &#8220;1 Euro&#8221;,rhs: &#8220;1.2692 U.S. dollars&#8221;,error: &#8220;&#8221;,icc: true}</strong></em></li>
<li>Using PHP&#8217;s <a href="http://php.net/manual/en/function.explode.php" target="_blank">explode</a> function, we separate <strong>lhs</strong> and <strong>rhs</strong> from the received response.</li>
<li>From <strong>rhs</strong> we get the converted value and multiply it by our dsired amount to get total conversion.</li>
<li>Using PHP&#8217;s <a href="http://php.net/manual/en/function.round.php" target="_blank">round</a> function, converted amount is round off to two digits.</li>
<li>After this we use PHP&#8217;s <a href="http://php.net/manual/en/function.ucwords.php">ucwords</a> function to Capitalize our text&#8217;s first letter.</li>
<li>We almost do the same for lhs.</li>
<li>At last we make results by joining lhs and rhs and echo it to give the results back to our AJAX function.</li>
</ul>
<h1>Last words</h1>
<p>In the source code I have applied some CSS to make the user interface better. So don&#8217;t be puzzled with the code in source file.</p>
<p>Finally, you have a working Live Currency Converter. You may use this script in your website to attract more visitors. Just play around the code and let me know if you can enhance it further. Please feel free to ask, your comments are always welcome.</p>
<p>If you like this article, please share it on your favorite website. It may help someone somewhere!</p>
<div id="_mcePaste" class="mcePaste" style="position: absolute; left: -10000px; top: 570px; width: 1px; height: 1px; overflow: hidden;">&lt;script type=&#8221;text/javascript&#8221;&gt;&lt;!&#8211;<br />
google_ad_client = &#8220;ca-pub-7217075623453662&#8243;;<br />
/* Tm &#8211; 336 x 280 */<br />
google_ad_slot = &#8220;2820701010&#8243;;<br />
google_ad_width = 336;<br />
google_ad_height = 280;<br />
//&#8211;&gt;<br />
&lt;/script&gt;<br />
&lt;script type=&#8221;text/javascript&#8221;<br />
src=&#8221;http://pagead2.googlesyndication.com/pagead/show_ads.js&#8221;&gt;<br />
&lt;/script&gt;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.techmug.com/ajax-currency-converter-with-google-api/feed/</wfw:commentRss>
		<slash:comments>40</slash:comments>
		</item>
		<item>
		<title>Detect Javascript and Flash Enabled</title>
		<link>http://www.techmug.com/detect-javascript-and-flash-enabled/</link>
		<comments>http://www.techmug.com/detect-javascript-and-flash-enabled/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 18:56:07 +0000</pubDate>
		<dc:creator>Fraz Ahmed</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[Detect Javascript]]></category>
		<category><![CDATA[Flash Enabled]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[swf]]></category>
		<category><![CDATA[tactic]]></category>

		<guid isPermaLink="false">http://www.techmug.com/?p=73</guid>
		<description><![CDATA[Javascript and Flash are super tools to enhance the usability of your website. You put a lot of efforts to create beautiful form validation and an attractive flash animation. But all the efforts are ruined when the visitor of your website has disabled Javascript or does not have flash plugin installed. Here I&#8217;ll provide you [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Javascript and Flash</strong> are super tools to enhance the usability of your website. You put a lot of efforts to create beautiful form validation and an <strong>attractive flash animation</strong>. But all the efforts are ruined when the visitor of your website has disabled Javascript or does not have flash plugin installed. Here I&#8217;ll provide you a little solution to escape from this problem.</p>
<p><span id="more-73"></span></p>
<h2>First Tactic &#8211; Detect Javascript Enabled</h2>
<p>Our first step is to detect whether visitor has Javascript enabled or not. If Javascript is not enable, we will redirect user to an error/instruction page <em>(I create a seperate page where I give instructions to visitor that how to enable Javascript in his/her browser)</em>.</p>
<p>Put below code just after the <em>&lt;head&gt;</em> tag in your web page:</p>
<pre class="brush: php; title: ; notranslate">
&lt;noscript&gt;
&lt;meta http-equiv=&quot;refresh&quot; content=&quot;0;URL=error-javascript.php&quot; /&gt;
&lt;/noscript&gt;
</pre>
<p>Above code is executed if Javascript is disabled and redirect user to <em>error-javascript.php</em>. We are using <em>&lt;meta&gt;</em> tag for redirection. If Javascript is not disabled, above code will not be executed.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-7217075623453662";
google_ad_slot = "2820701010";
google_ad_width = 336;
google_ad_height = 280;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<center><font-size="9px">Advertisement</font></center></p>
<h2><strong>Second Tactic- Detect Flash Plugin Installed</strong></h2>
<p>There is a<strong> Javascript solution to detect Flash Plugin</strong> Installed and replace Flash animation with your custom message. In order to accomplish the said task you need to place below Javascript instead of your normal embedded code:</p>
<pre class="brush: jscript; title: ; notranslate">
&lt;script type=&quot;text/javascript&quot;&gt;
&lt;!--
var MM_contentVersion = 9;
var plugin = (navigator.mimeTypes &amp;&amp; navigator.mimeTypes[&quot;application/x-shockwave-flash&quot;]) ? navigator.mimeTypes[&quot;application/x-shockwave-flash&quot;].enabledPlugin : 0;
if ( plugin ) {
		var words = navigator.plugins[&quot;Shockwave Flash&quot;].description.split(&quot; &quot;);
	    for (var i = 0; i &lt; words.length; ++i)
	    {
		if (isNaN(parseInt(words[i])))
		continue;
		var MM_PluginVersion = words[i];
	    }
	var MM_FlashCanPlay = MM_PluginVersion &gt;= MM_contentVersion;
}
else if (navigator.userAgent &amp;&amp; navigator.userAgent.indexOf(&quot;MSIE&quot;)&gt;=0
   &amp;&amp; (navigator.appVersion.indexOf(&quot;Win&quot;) != -1)) {
	document.write('&lt;SCR' + 'IPT LANGUAGE=VBScript\&gt; \n'); //FS hide this from IE4.5 Mac by splitting the tag
	document.write('on error resume next \n');
	document.write('MM_FlashCanPlay = ( IsObject(CreateObject(&quot;ShockwaveFlash.ShockwaveFlash.&quot; &amp; MM_contentVersion)))\n');
	document.write('&lt;/SCR' + 'IPT\&gt; \n');
}
if ( MM_FlashCanPlay ) {
	document.write('&lt;object classid=&quot;clsid:d27cdb6e-ae6d-11cf-96b8-444553540000&quot; codebase=&quot;http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0&quot; width=&quot;160&quot; height=&quot;120&quot;&gt;');
    document.write('&lt;param name=&quot;movie&quot; value=&quot;animation.swf&quot;&gt;');
    document.write ('&lt;param name=&quot;quality&quot; value=&quot;high&quot;&gt;');
	document.write ('&lt;param name=&quot;wmode&quot; value=&quot;transparent&quot;&gt;');
    document.write('&lt;embed src=&quot;animation.swf&quot; width=&quot;160&quot; height=&quot;120&quot; quality=&quot;high&quot; pluginspage=&quot;http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash&quot; type=&quot;application/x-shockwave-flash&quot; wmode=&quot;transparent&quot;&gt;&lt;/embed&gt;&lt;/object&gt;');
} else{
	document.write('&lt;a href=&quot;./&quot;&gt;&lt;img src=&quot;replaced-image.png&quot; alt=&quot;Flash Plugin not Installed&quot; border=&quot;0&quot;&gt;&lt;/a&gt;');

}
//--&gt;

&lt;/script&gt;
</pre>
<p>Above code detect for Version 9 or greater version of flash plugin. If flash plugin is detected it displays flash animation <em>(animation.swf)</em> else it displays an image <em>(replaced-image.png)</em> instead of flash animation. In the place of image you can write more text and instructions. Everything is upto on you.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-7217075623453662";
google_ad_slot = "4831550702";
google_ad_width = 300;
google_ad_height = 250;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<center><font-size="9px">Advertisement</font></center></p>
<p>Don&#8217;t forget to share the post on your favorite website. It may also help someone somewhere.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techmug.com/detect-javascript-and-flash-enabled/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>

