Welcome to WebmasterWorld Guest from 54.167.40.25

Forum Moderators: open

Message Too Old, No Replies

Javascript Currency Converter

that grabs exchange rates from a file

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

New User

10+ Year Member

joined:Mar 6, 2003
posts:15
votes: 0


Hello,

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)

Junior Member

10+ Year Member

joined:Dec 27, 2002
posts:125
votes: 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?
3:20 am on Dec 10, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 21, 2002
posts:977
votes: 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)

New User

10+ Year Member

joined:Mar 6, 2003
posts:15
votes: 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"
src="rates.js"></script>

Then what?

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

Senior Member

WebmasterWorld Senior Member blobfisk is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Feb 25, 2002
posts:3185
votes: 0


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

USD2AUD = 1.1;
EUR2USD = 1.5;
etc.

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)

New User

10+ Year Member

joined:Mar 6, 2003
posts:15
votes: 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)

Senior Member

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 15, 2002
posts:6807
votes: 0


Are the pages dynamic?
5:03 am on Dec 12, 2003 (gmt 0)

New User

10+ Year Member

joined:Mar 6, 2003
posts:15
votes: 0


nope. just plain old html.
3:16 pm on Dec 12, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 15, 2002
posts:6807
votes: 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)

New User

10+ Year Member

joined:Mar 6, 2003
posts:15
votes: 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!