PDA

View Full Version : URGENT HELP IN: Calling a function in an external.js file


habib
09-18-2002, 04:38 PM
Hi,

I have created some functions in an external.js file. I now want to use them in my htm files to display prices in various currencies. I used the following syntax to call the function:

<script language ="Javascript" src="amount.js">
document.write(convert_amount(10));
</script>

but it didn't display any amount.

The code for amount.js is as follows:

function getCookie(name){
var cname = name + "=";
var dc = document.cookie;
if (dc.length > 0) {
begin = dc.indexOf(cname);
if (begin != -1) {
begin += cname.length;
end = dc.indexOf(";", begin);
if (end == -1) end = dc.length;
return unescape(dc.substring(begin, end));
}
}
return null;
}

function convert_amount (amount) {
var input_amount = amount;
var sa_rate = 15;
var us_rate = 1.4;
var eu_rate = 1.6;

if (getCookie('home_location') == 'UK') {
return '£'+(input_amount); }
else
if (getCookie('home_location') == 'SA') {
return 'R'+(input_amount * sa_rate); }
else
if (getCookie('home_location') == 'US') {
return '$'+(input_amount * us_rate); }
else
if (getCookie('home_location') == 'EU') {
return 'E'+(input_amount * eu_rate); }
}

But when I copied the function from amount.js and pasted it into the <head> of the htm file and then use the following syntax:

<script language ="Javascript">
document.write(convert_amount(10));
</script>

it works like a dream!! I get the price '10' in GBP, when I change the country of residence it displays the price accordingly:

Europe = Euros
South Africa = SA Rands
United Kingdom = GBP
United States = USD

I want to use the function on lots of htm files where the 'amount_value' will keep on changing. How do I get it to work???
Is there some syntax I have to have in the external file that might be missing?


:confused:

MikoLone
09-18-2002, 04:48 PM
<script language ="Javascript" src="amount.js">
document.write(convert_amount(10));
</script>

I think your problem will be solved if you put this into the head tag

<script language ="Javascript" src="amount.js"></script>


then in the body tag somewhere put
<script>convert_amount(10);</script>

if you need to write something to the page, I would write it into the .js file. Maybe at the end of the function.

Good luck :thumbsup:

MikoLone
09-18-2002, 05:09 PM
only if some of the variable names are the same. If there are some that are the same them they will clash. You can put it anywhere you want really. I just perfer to put them in the head tag. Many times I just write all of my functions in one .js file. Try it out.

habib
09-18-2002, 05:11 PM
I have just tried that, but it still doesn't write the amount onto the page. I tried using the document.write(convert_amount(10)); at the end of the script, but this will only display the amount 10, and not all the amounts are 10. Some of the amounts are 20, 30 and so on. How can I call the function from the external file and pass it different values for the amount on different webpages?

habib
09-18-2002, 05:25 PM
Thanks a million MikoLone, its actually working:)

Wow!!!!

Thats great!

Now, my next problem is how can I display the amount upto 2 decimal places.

For example I use the initial value of £14 for a product, the function converts this to E22.400000000000002 based on an exchange rate of £1 = E 1.6 How do I get the amount to be displayed to E22.40?