0

Hi im using following code to get the query string value in a form field:

<script>
var url = window.location.search;
url = url.replace("?Id=", ''); // remove the ? 
</script>
<script>document.write("<input name='Id' data-constraints='@Required(groups=[customer])' id='Id' type='text' value='"+url+"' />");</script>

if i visit [this is not a link] (http://mydomain.com/?Id=987) it records the Id in field but when more parameters are included it also records in the field, i want only first or any specific query string parameter to be added in form field?

2

2 Answers 2

2

How about building an nice little associative array out of your query string parameters, something like:

var urlParams = [];
window.location.search.replace("?", "").split("&").forEach(function (e, i) {
    var p = e.split("=");
    urlParams[p[0]] = p[1];
});

// We have all the params now -> you can access it by name
console.log(urlParams["Id"]);
Sign up to request clarification or add additional context in comments.

Comments

0

Basically, you need to check for the existence of an ampersand, the get the sub string up to the ampersand.

You could add this code after you do your replace:

var start = url.indexOf('&');

if(start > 0) {
    url = url.substring(0, start);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.