Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

This page (http://forums.iis.net/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?

like image 318
user1647051 Avatar asked Mar 14 '26 03:03

user1647051


1 Answers

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.org/wiki/Cross-site_scripting vulnerability. To avoid that, you must escape the HTML before you output it on the screen

like image 59
Juan Mendes Avatar answered Mar 16 '26 17:03

Juan Mendes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!