How do I auto-populate a text field from URL Parameter using Javascript - Stack Overflow

This page (.aspx) explains how to do exactly what I am wanting to do, except due to the limitations of

This page (.aspx) explains how to do exactly what I am wanting to do, except due to the limitations of our system I only get to enter javascript to pull this information into the textbox. Can someone explain how I can use a parameter in a URL to auto fill a textbox?

This page (http://forums.iis/t/1153336.aspx) explains how to do exactly what I am wanting to do, except due to the limitations of our system I only get to enter javascript to pull this information into the textbox. Can someone explain how I can use a parameter in a URL to auto fill a textbox?

Share Improve this question edited Sep 4, 2012 at 18:35 woz 11k3 gold badges35 silver badges65 bronze badges asked Sep 4, 2012 at 18:33 user1647051user1647051 411 gold badge1 silver badge2 bronze badges 2
  • Show what the parameters are likely to be, show the input (there's no such thing as a 'textbox' element) that it'll be populating. And, since you have a tutorial explaining how to do what you want in one language, could you explain the problems you're having translating it to JavaScript, where are you stuck? – David Thomas Commented Sep 4, 2012 at 18:35
  • Sorry I was not clear. I am very new to programming in general, and don't know JavaScript at all, so I din't really know where to start in converting it. The reason I was forced to use JavaScript in this situation is that I have to enter the code into a proprietary system, and the UI is limited to one input at a time and using JavaScript only. – user1647051 Commented Sep 4, 2012 at 19:44
Add a ment  | 

4 Answers 4

Reset to default 10

You can use location.search to access the url query and inject it into your text value

If your url is /page.htm?x=y, you can use the following code to set the value of a text field. This post tells you how to grab values from the query string

// See the link above for getUrlParams
var params = getUrlParams();
document.getElementById('myTextFieldId').value = params.x;

Notice that the example you've linked to suffers from a http://en.wikipedia/wiki/Cross-site_scripting vulnerability. To avoid that, you must escape the HTML before you output it on the screen

You should be able to do something similar using windows.location in js

http://css-tricks./snippets/javascript/get-url-and-url-parts-in-javascript/

You can easily use the answer of this question to get parameters from the URL using JavaScript. Then set the value of your textboxes like this:

this.form.elements["element_name"].value = 'Some Value';

If you want a quick solution, you might be interested in using this code snippet (inspiration taken from here, but modified by myself):

function getURLParameter(e) {
  return decodeURI((new RegExp(e + "=(.+?)(&|$)").exec(location.search) || [, ""])[1]);
}

if (getURLParameter("inputKey") === "") {
  console.log("No URL param value found.");
} else {
  document.getElementById("inputBox").value = getURLParameter("inputkey");
}

Let's say that your url is example.?inputKey=hello world. If so, using the above code will fill in the input with the ID of "inputBox" with the phrase "Hello world". If the URL was just example., it wouldn't prefill the box, and instead just act as though there was nothing (show the placeholder etc).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743615082a4478859.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信