Thursday, June 14, 2007

Working on some Javascript

Right now I'm working on a way to have live stock quotes in my blog. When I write a post and want to have the quote shown, I use a tag like <stockquote>GE</stockquote> or <stockquote symbol="DUK">Duke Energy</stockquote>. When the page loads, the Javascript searches through all the <stockquote> elements and composes a list of symbols. It contacts a PHP script on my own web server, which passes the request on to Yahoo Finance. It gets back a CSV list of the data and parses it into Javascript objects. The Javascript then goes back through the page and updates the <stockquote> tags with the price data.

The mechanism is convoluted because browsers don't let you create an AJAX request to other servers. Otherwise the Javascript could request the stock data directly from Yahoo Finance. It sucks, but it works! Here are some samples:

<stockquote symbol="PCU">Southern Copper (PCU)</stockquote> produces Southern Copper (PCU)

Well, Internet Explorer threw a wrench into my plans. I did all my testing in Firefox and was very pleased, but it turns out Internet Explorer doesn't support "fake" tags like <stockquote> very well. I wasn't able to dynamically change the content of the tag. So I've changed it to <span symbol="blah"> and now it works in both!

1 comment:

Anonymous said...

I'm a web designer and I found your article to be quite interesting. Please post the Javascript code that does the stuff you described in your article. Thanks.