joibrooks
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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@leakim971 that worked except I'm getting too much information passed through. This was in the input field:
myemail@address.com%3fEMAI L%3d%40EMA IL%40
How can I strip the rest of the information out? Sample of the code I used is attached.
Thank you!
myemail@address.com%3fEMAI
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>
Thank you!
Why do you put all this information?
Parameter in a URL must be separate by the << & >>
Parameter in a URL must be separate by the << & >>
ASKER
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%3fEMAI L%3d%40EMA IL%40
%3fEMAIL%3d%40EMAIL%40 is equal to ?EMAIL=@EMAIL@
Have I made myself clear?
So, in this URL...
http://www.URL.com/?EMAIL=@EMAIL@
The script is capturing:
myemail@address.com%3fEMAI
%3fEMAIL%3d%40EMAIL%40 is equal to ?EMAIL=@EMAIL@
Have I made myself clear?
ASKER
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!!
http://stackoverflow.com/questions/4877966/pass-a-simple-url-variable-into-and-input-field-via-javascript
Open in new window