javascript - How to get url data in html textbox -


i have simple code of javascript displays text url. example: index.html?id=001 output displays in html page 001. want data (id=001) html text box display "001" textbox.

here code.

<script type="text/javascript"> function get() { var data = []; for(x = 0; x < arguments.length; ++x) data.push(location.href.match(new regexp("/\?".concat(arguments[x],"=","([^\n&]*)")))[1]) return data; } </script>  <br /> <script type="text/javascript"> document.write(get("id")[0]); </script> <br /><br /> <input type="text" id="" value="" name="" /> 

actually want "001" textbox textbox value.

regular expressions overkill this, , expression find querystring value if follows ?.

you should use location.search rather full url, because need take after first character (which ? if there anything), , eliminate hashes.

i used simple string splitting create reusable map of key/value pairs lookup.

function get() {    var data = [];    (var = 0; < arguments.length; i++) {      data.push(getquerymap()[arguments[i]]);    }    return data;  }    var querymap = null;  function getquerymap() {    if (querymap) { return querymap; }    querymap = {};    var querysplit = location.search.substring(1).split('&');    (var = 0; < querysplit.length; i++) {      var thisquery = querysplit[i].split('=', 2);      querymap[thisquery[0]] = thisquery[1];    }    return querymap;  }

to use input , value it, need id on or way identify it:

<input type="text" id="someinput" value="" name="" />

then, after it, put script so:

<script type="text/javascript">    document.getelementbyid('someinput').value = get('id')[0];    </script>


Comments