-1

I'm trying to write some javascript to pick up a currency query in the URL and then use the existing Ajax post method from the website to load up the correct currency depending on what URL was linked to.

my-url.com/?currency=USD <- loads the site in US Dollars 
my-url.com/?currency=GBP <- loads the site in British Pounds 
my-url.com/?currency=EUR <- loads the site in EUROS

This is the stock code for the currency selector to change the currency on the website:

$('#currency').on 'change', ->
    $.ajax(
      type: 'POST'
      url: $(this).data('href')
      data:
        currency: $(this).val()
).done ->
window.location.reload()

This is what I have done so far:

if (window.location.href.indexOf("?currency=EUR") >= 0) {

    $.ajax(
        type: 'POST'
        url: $(this).data('href')
        data:
        currency: $(this).val()
    ).done 

}

It does not work as I am not pulling in the currency into the post method, and probably other reasons too.

This is beyond my scope, would anyone know how I would go about setting the JS to check on load for a currency in the url, set that to the currency value in the script and POST?

Liam
  • 27,717
  • 28
  • 128
  • 190
Matt Kennedy
  • 415
  • 3
  • 9
  • What exactly is the question here? You sound like your out of your depth and are just hoping for someone to do (something?) for you. This isn't a free code writing service. You're expected to ask answerable questions? – Liam Feb 16 '18 at 14:03
  • 1
    So what is the problem? `$(this)` is going to be `window`, I highly doubt you have data on the window object, – epascarello Feb 16 '18 at 14:03
  • Reading it again, is this what you want? [How can I get query string values in JavaScript?](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Liam Feb 16 '18 at 14:07
  • Yeah, how do I get the currency query into a variable, considering that the currency might not always be the last part of the url, or may have further queries like location. – Matt Kennedy Feb 16 '18 at 14:34
  • right, so this is a duplicate then? – Liam Feb 16 '18 at 14:39
  • Ive taken the stock currency selector script and went to mould that into some script that checks fo r a url query and sets the correct currency on load. I guess my first real question is.. Is it possible to set query parameters to a variable in javascript? – Matt Kennedy Feb 16 '18 at 14:41
  • Possible duplicate of [How can I get query string values in JavaScript?](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Matt Kennedy Feb 16 '18 at 14:48

1 Answers1

-1

If the currency is three characters long and its the last query at the end of url, you can try this:

window.location.href.substring(window.location.href.length-3)     

It takes the href value and removes everything else but the last three characters at the end.

P Roitto
  • 1,533
  • 1
  • 11
  • 9
  • Thanks, That helps somewhat, I think my first step is having the url checked for currency in a query and then taking that and setting it as a variable. Your method works if the currency query is the last part of the url. Is there a more sure fire way of setting a js variable to the content of a url query. – Matt Kennedy Feb 16 '18 at 14:36