I'm trying to fill a textbox value using JavaScript after a redirect is done. The scenario is the following:
The user clicks on a button, which will redirects the user to the contact page with a contact form. Then some contact textboxes like e.g. "subject" should already be filled. I'm using this code for my button:
<input type="button" name="submit" id="submit" value="Submit" onClick="RegisterCourse('test')" />
And this is my JavaScript code:
function RegisterCourse(title){
window.location.href = "";
document.getElementsByName("your-subject")[0].value = title; // your-subject is the textbox name
}
I think this isn't working because of the redirect. When I output document.getElementsByName("your-subject").length
, it returns "0".
Is this even possible? If yes, how can I achieve this? If not, are there any alternatives?
I'm trying to fill a textbox value using JavaScript after a redirect is done. The scenario is the following:
The user clicks on a button, which will redirects the user to the contact page with a contact form. Then some contact textboxes like e.g. "subject" should already be filled. I'm using this code for my button:
<input type="button" name="submit" id="submit" value="Submit" onClick="RegisterCourse('test')" />
And this is my JavaScript code:
function RegisterCourse(title){
window.location.href = "http://www.mydomain.ch/contact";
document.getElementsByName("your-subject")[0].value = title; // your-subject is the textbox name
}
I think this isn't working because of the redirect. When I output document.getElementsByName("your-subject").length
, it returns "0".
Is this even possible? If yes, how can I achieve this? If not, are there any alternatives?
Share Improve this question edited Oct 4, 2015 at 13:13 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Sep 12, 2015 at 10:30 TylerTyler 8825 gold badges18 silver badges33 bronze badges 1- You can certainly do it, The question is, what is your targeted browsers ? Based on that you can use several mediums to pass data along the redirection ( assuming the redirection is not to an external domain ) such as: cookies, local storage, session storage etc.. – elad.chen Commented Sep 12, 2015 at 10:41
1 Answer
Reset to default 8This cannot work, because after redirecting, the JavaScript
- can't get executed anymore.
- can't change a control on another page.
One idea could be to add a URL parameter to the redirect and then, on the new page, check for this URL parameter and if set, then set the control with this parameter value.