Link to home
Start Free TrialLog in
Avatar of joibrooks
joibrooksFlag for United States of America

asked on

Simple javascript that loads a URL variable into a form input tag

I'm sending an email campaign. There will be a link from the email campaign that sends subscribers to a landing page. The landing page link from the email will include the email address of the subscriber through a variable. Something like this:
http://www.URL.com/?EMAIL=@EMAIL@

The landing page will have a single input field and a submit button. I'd like to pre-populate that field with the email address from the URL using javascript.

How can I do this?
Avatar of Randy Downs
Randy Downs
Flag of United States of America image

Try this
http://stackoverflow.com/questions/4877966/pass-a-simple-url-variable-into-and-input-field-via-javascript

document.getElementById("input_id").value = location.search.replace('?','');

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joibrooks

ASKER

@leakim971 that worked except I'm getting too much information passed through. This was in the input field:

myemail@address.com%3fEMAIL%3d%40EMAIL%40

How can I strip the rest of the information out? Sample of the code I used is attached.

<script language="javascript">
var gup = function( name ) {
    var results = (new RegExp("[\\?&]"+name+"=([^&#]*)")).exec(window.location.href);
    if ( results == null ) {return ""}
    else {return results[1]}
};

window.onload = function() {
    document.getElementById("emailaddie").value = gup("EMAIL");
}
</script>

Open in new window


Thank you!
Why do you put all this information?
Parameter in a URL must be separate by the << & >>
It looks like the script is not parsing the data that includes and follows the << ? >>.


So, in this URL...
http://www.URL.com/?EMAIL=@EMAIL@

The script is capturing:
myemail@address.com%3fEMAIL%3d%40EMAIL%40

%3fEMAIL%3d%40EMAIL%40 is equal to ?EMAIL=@EMAIL@

Have I made myself clear?
Hold on! I've found the error. You were correct in asking me why so much info. There was a typo. The solution is fine! Thank you!!