homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

Javascript Currency Converter
that grabs exchange rates from a file

 12:04 am on Dec 10, 2003 (gmt 0)


Anyone know a way I can use Javascript to put a USD value below the AUD value on my site, below each product, using an exchange rate stored in a database or file (updated by myself daily)

I don't actually know Javascript language so I need a pretty detailed explanation (I have been told that Javascript could do this function though). Thanks.



 12:21 am on Dec 10, 2003 (gmt 0)

Javascript can not read in contents of a file. I can't think of an elegant Javascript solution, apart from using frames. I think you'll have use server side scripting, unless someone has a solution?

Purple Martin

 3:20 am on Dec 10, 2003 (gmt 0)

It will work if the file you update yourself has a .js extension. In other words, use a JavaScript library file!

The file only needs one line with a variable declaration, for example:

var exRate = 2.13

Now link to the file in the head of your document, and add the JavaScript to do the calculation.


 6:47 am on Dec 10, 2003 (gmt 0)

Ok bear with me here...

I can create the .js file, for example rates.js which I upload to somewhere.

Then I stick the following (?) under <head> in the html;

<script type="text/javascript"

Then what?


 3:00 pm on Dec 10, 2003 (gmt 0)

The rates.js file would have a variable decleration with the current currency exchange rate. EG:

USD2AUD = 1.1;
EUR2USD = 1.5;

Then you can use these variables in your page to do you currency conversion.

Javascript is probably not the best way to do currency conversion. The best way would be to subscribe to a rates feed (XML, for example) and use a server side solution to provide accurate and up to date currency conversions.

XE.com [xe.com] provides both free and paid for services that may help.


 11:47 pm on Dec 11, 2003 (gmt 0)

I've had a look at XE and all the options require the customer to have to click on 'convert' to see the US price. Plus I don't have room to stick their converter under each product. I want the US price to be displayed at all times beneath my AUD price.


 4:54 am on Dec 12, 2003 (gmt 0)

Are the pages dynamic?


 5:03 am on Dec 12, 2003 (gmt 0)

nope. just plain old html.


 3:16 pm on Dec 12, 2003 (gmt 0)

What you can easily do (which will work in any browser supporting JavaScript) is:

1) Create a file that has something like this in it:

exchRate = 1.3;
function getUSD(price) {
document.write(price * exchRate);

2) Load this script on every page as an external JavaScript

3) Where ever you want the USD price to be displayed, simply do:

<script type="text/javascript"> getUSD('5.90'); </script>

...where 5.90, in this case, would be the AUD price.

4) This way, you only need to update one file (the external JavaScript) with the new exchRate.


 4:07 am on Dec 13, 2003 (gmt 0)

Brilliant... the script works! Thanks DrDoc.

I'm so happy, thanks for all your help. I also dug up a script to round the numbers to two decimal places, which also works great.

Bravo Javascript!

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved