html - Set input type text value after redirecting using JavaScript - Stack Overflow

I'm trying to fill a textbox value using JavaScript after a redirect is done. The scenario is the

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
Add a ment  | 

1 Answer 1

Reset to default 8

This cannot work, because after redirecting, the JavaScript

  1. can't get executed anymore.
  2. 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.

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信