0

I'm currently trying to extract from ReturnUrl= ... I want to extract the URL from the link below using javascript. Can anyone help?

http://testdealbuilderCCMS/questionnaire.aspx?db_template_reference=Construction: Westfield Services Agreement&ContractDescription=Facilities Contract&NatureServices=FACILITIES&SiteDescription=Retail Units&ThirdPartyAgreementsList=&ServiceFee=1000&ReturnUrl=http://localhost:4965&launcher.aspx?directLink=PX&caseKey=7ccef65756504a79bc3a4a6687c0d9555e519ec9079241c9944c6a523704&PXid=
theron
  • 7
  • 1
  • 7
  • possible duplicate of [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Joe Fitter Feb 19 '15 at 12:04
  • 1
    `var extractedUrl = originalUrl.replace( /&ReturnUrl=([^]+)(|$)/, "$1" )` would be the method by the book. However, that assumes that all occurrences of `&,#` in the ReturnUrl parameter are properly percent-encoded ( `%26`, `%23` ), but they typically aren't. Do you have more information about the structure of ReturnUrl and the embedding url? – collapsar Feb 19 '15 at 12:06

1 Answers1

1

there are lots of edge cases here that will make this fail. So be careful, use this only if your string always ends with the ReturnURL parameter.

Find the position of the ReturnURL= in the string then get the substring from ReturnURL= position + ReturnURL= length, to the end.

http://jsfiddle.net/3hvajedg/1/

the_string = 'http://testdealbuilderCCMS/questionnaire.aspx?db_template_reference=Construction: Westfield Services Agreement&ContractDescription=Facilities Contract&NatureServices=FACILITIES&SiteDescription=Retail Units&ThirdPartyAgreementsList=&ServiceFee=1000&ReturnUrl=http://localhost:4965&launcher.aspx?directLink=PX&caseKey=7ccef65756504a79bc3a4a6687c0d9555e519ec9079241c9944c6a523704&PXid=';

alert(the_string.substring((the_string.indexOf('ReturnUrl=')+'ReturnUrl='.length)));
Sharky
  • 6,154
  • 3
  • 39
  • 72