2

I have a url

http://www.example.com/state=survey&action=display&survey=39&zone=surveys&currency=Denmark

A user will land on a new page with the above url. This page will include a form. There is an input with an id 'currencyrequired' - i want this input to automatically pull in the currency variable from the url (in this example Denmark).

<input type="text" id="currencyrequired" name="currencyrequired" size="40" maxlength="20" value="" class="input-pos-int">

The currency part of the url is likely to change as it depends upon which country they select so you could end up with - http://www.example.com/state=survey&action=display&survey=39&zone=surveys&currency=Norway

Ideally what i would like is to list all the currencies (8 in total) and the script would check what country is listed in the url and populate the currencyrequired input field with the output

R4N_S_S
  • 19
  • 1
  • 6
  • I need to get this fixed _Yesterday_?? – Dhaval Marthak Apr 24 '13 at 09:20
  • possible duplicate of [How to get the value from URL Parameter?](http://stackoverflow.com/questions/979975/how-to-get-the-value-from-url-parameter) – Quentin Apr 24 '13 at 09:26
  • @TheVal - this project is due this week and i need to get this functionality rolled out asap hence the urgency. Im a relative novice with javascript/jquery so any help is appreciated – R4N_S_S Apr 24 '13 at 10:08

2 Answers2

0

You could use location.search to return the query string and do:

var url = location.search;
var country = url.substring(url.indexOf('currency=')+9, url.length);

$("#input").val(country);

http://jsfiddle.net/ckJ5S/

Darren
  • 68,902
  • 24
  • 138
  • 144
  • 1
    That will only work if currency=Denmark is the last information in the URL. – TryingToImprove Apr 24 '13 at 09:30
  • This works in the jsfiddle but when i aste the code into my page it doesnt do anything. – R4N_S_S Apr 24 '13 at 10:13
  • @R4N_S_S - do you have jquery defined on your page? Try adding an alert. `alert(country);` - does the alert work? – Darren Apr 24 '13 at 10:16
  • The currency part of the url is likely to change as it depends upon which country they select so you could end up with - http://www.example.com/state=survey&action=display&survey=39&zone=surveys&currency=Norway – R4N_S_S Apr 24 '13 at 10:25
  • @Darren - yes jquery is present on the page. the alert works - thanks in advance – R4N_S_S Apr 24 '13 at 10:30
  • As the url is dynamically generated i guess this is the reason why they are not populating the input field. Is there any way of listing the available currencies in something like an array and then the scripts checks what variable is present and places that into the currencyrequired input box? – R4N_S_S Apr 24 '13 at 10:33
0

Try this:

$("#currencyrequired").val(getParameterByName('currency'));

function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.search);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}
sdagkas
  • 578
  • 1
  • 5
  • 20