最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论