Hack 26. Convert Currencies with One Click

Yahoo! Finance can tell you how many euros there are in a U.S. dollar, and a little JavaScript can speed up the answer.
While browsing the Web, you might find yourself in some unusual places. You're potentially just a click away from any site that exists, including online stores large and small, across the globe. The next time you're reading about a bleeding-edge European cell phone from your home in the United States, you might want to find out just how much that £500 would be in U.S. dollars.
Yahoo! Finance's Currency Converter (http://finance.yahoo.com/currency) can give you the answer. From this page, you can select a currency to convert from, a currency to convert to, and the amount of currency you'd like to convert.
This is a fairly painless process, but it still involves leaving your review site, opening a new browser window, and generally losing focus from all of those cutting-edge features of that smart phone you're really interested in.
Because Yahoo!'s Currency Converter handles everything in the URL query string, you can write some JavaScript to automate this process and find out what you would need to pay, with just one click. For example, the following URL goes directly to the Yahoo! page that converts £500 into U.S. dollars:
- http://finance.yahoo.com/currency/convert?amt=500&from=GBP&to=USD
The query string variables are fairly self-explanatory:
amt
-
The amount of currency to convert
from
-
The three-letter abbreviation for the currency you're converting from
to
-
The three-letter abbreviation for the currency you're converting to
By changing these values in the URL, you can change the results page. Here's the URL for converting 100 U.S. dollars into British pounds:
- http://finance.yahoo.com/currency/convert?amt=100&from=USD&to=GBP
Here's just a handful of the 157 currencies Yahoo! currently supports:
USD
-
U.S. dollar
EUR
-
Euro
CAD
-
Canadian dollar
AUD
-
Australian dollar
JPY
-
Japanese yen
INR
-
Indian rupee
NZD
-
New Zealand dollar
CHF
-
Swiss franc
ZAR
-
South African rand
You can find a list of all the currencies and abbreviations that Yahoo! supports in the drop-down list at the Currency Converter page.
2.5.1. The Code
This hack will let you highlight a currency amount on a web page and convert it from British pounds to U.S. dollars. It's a piece of JavaScript that resides in a browser bookmark, otherwise known as a bookmarklet. Bookmarklets run when you click the bookmark, and they can get information from the current page you're browsing. In this case, the information the bookmarklet gets is the currency amount you've highlighted.
The code for bookmarklets isn't very pretty to look at, so here's some nicely formatted JavaScript that approximates the bookmarklet functions:
// Dissected JavaScript bookmarklet for one-click Currency Conversion
// Set d to the document object as a shortcut
var d = document;
// Set t to the currently selected text, if available
var t = w.selection?w.selection.createRange( ).text:w.getSelection( );
// Test to make sure t is a number
t = parseFloat(t);
If (t != parseFloat(t)) {
// If not, warn that the value isn't numeric
alert('Please highlight a numeric value.');
} else {
// Build the URL
var url = http://finance.yahoo.com/currency/convert?';
url += 'amt='+escape(t)+'&';
url += 'from=GBP&';
url += 'to=USD';
// And open in a new window
window.open(url,
'_blank',
'width=480,height=440,status=yes,resizable=yes,scrollbars=yes');
}
Because bookmarklets are compact, this is the actual code you'll need to use:
javascript:d=document;t=d.selection?d.selection.createRange( ).text:d.
getSelection( );t=parseFloat(t);if(t!=parseFloat(t)){alert('Please
highlighta numeric value.')}else{url='http://finance.yahoo.com/
currency/convert?amt='+escape(t)+'&from=GBP&to=USD';void(window.open(url,
'_blank','width=480,height=440,status=yes,resizable=yes,scrollbars=yes'))}
2.5.2. Running the Hack
Running the code is just a matter of adding a bookmark to your preferred browser. Once a new bookmark exists, replace the URL with the JavaScript. Also, give the bookmarklet a descriptive name, such as GBP to USD. Once the bookmarklet is set, when you want to perform a currency conversion, you can highlight a numeric value on any page and click the bookmark. For example, Figure 2-11 shows a mobile phone site that mentions a price of £500.
Highlighting the amount and clicking the GBD to USD bookmark opens a new window with the conversion at Yahoo! Finance, as shown in Figure 2-12.
Now you know that (at the time of this writing) £500 equals US$890.47. But more importantly, you'll have this information a click away the next time you're browsing exotic locales across the Web.
 |