简体   繁体   中英

How to switch between two currencies by reloading javascript?

I have a javascript for counting price (it depends on many factors). I would like to have two currencies: euros and dollars. Currently the default one is euro. I would like to have two buttons for switching between them - one for euros and one for dollars. I imagine it would work like this: I would make two javascripts (I already have one of them - in which I calculate the price - let's call him europrice.js). Now I would create another one called dollarprice.js in which I would calculate the price in dollars. In default the website would have loaded with europrice.js. By clicking on "dollarbutton" the price would have reloaded this time with dollarprice.js and not europrice.js. I have tried to make this clear as much as I can, hopefully you understand :) Thanks for any advice

one point is, you may not need own js file, maybe a own function would be enough.

Apart from that you could wrap your implementation by an api-like call, and switching the implementation on click:

var PriceCalculator = {
    calculateInEuros: function() {
        //your euro logic here
    },
    calculateInDollars: function() {
        //your dollar logic here
    },
    // "api-function" - default: euro
    calculatePrice: PriceCalculator.calculateInEuros
}

usage:

var price = PriceCalculator.calculatePrice();

switching:

PriceCalculator.calculatePrice = PriceCalculator.calculateInEuros;

or

PriceCalculator.calculatePrice = PriceCalculator.calculateInDollars;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM