Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8450651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:01:36+00:00 2026-06-10T11:01:36+00:00

I am trying to fill out the fields on a form through JavaScript. The

  • 0

I am trying to fill out the fields on a form through JavaScript. The problem is I only know how to execute JavaScript on the current page so I cannot redirect to the form and execute code from there. I’m hesitant to use this term, but the only phrase that comes to mind is cross-site script. The code I am attempting to execute is below.

<script language="javascript"> 

window.location = "http://www.pagewithaform.com";

loaded();

//checks to see if page is loaded. if not, checks after timeout.
function loaded()
{
    if(window.onLoad)
    {
      //never executes on new page. the problem
      setTitle();
    }
    else
    {
      setTimeout("loaded()",1000);
      alert("new alert");
    }
}

//sets field's value
function setTitle()
{
    var title = prompt("Field Info","Default Value");
    var form = document.form[0];
    form.elements["fieldName"].value = title;
}
</script>

I’m not truly sure if this is possible. I’m also open to other ideas, such as PHP. Thanks.

EDIT: The second page is a SharePoint form. I cannot edit any of the code on the form. The goal is to write a script that pre-fills most of the fields because 90% of them are static.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:01:38+00:00Added an answer on June 10, 2026 at 11:01 am

    You’re trying to maintain state between pages. Conventionally there are two ways to maintain state:

    • Store state in cookies
    • Store state in the query string

    Either way your first page has to persist state (to either cookies or the query string) and the other page has to – separately – restore the state. You can’t use the same script across both pages.

    Example: Using Cookies

    Using cookies, the first page would have to write all the form data you’ll need on the next page to cookies:

    <!DOCTYPE html>
    <html>
     <head>
         <title>Maintaining State With Cookies</title>
     </head>
     <body>
         <div>
             Setting cookies and redirecting...
         </div>
         <script>
             // document.cookie is not a real string
             document.cookie = 'form/title=My Name is Richard; expires=Tue, 29 Aug 2017 12:00:01 UTC'
             document.cookie = 'form/text=I am demoing how to use cookies in JavaScript; expires=Tue, 29 Aug 2017 12:00:01 UT';
             setTimeout(function(){
                 window.location = "./form-cookies.html";
             }, 1000);
         </script>
     </body>
    </html>
    

    … and the second page would then read those cookies and populate the form fields with them:

    <!DOCTYPE html>
    <html>
     <head>
         <title>Maintaining State With Cookies</title>
     </head>
     <body>
         <form id="myForm" action="submit.mumps.cgi" method="POST">
             <input type="text" name="title" />
             <textarea name="text"></textarea>
         </form>
         <script>
             var COOKIES = {};
             var cookieStr = document.cookie;
             cookieStr.split(/; /).forEach(function(keyValuePair) { // not necessarily the best way to parse cookies
                 var cookieName = keyValuePair.replace(/=.*$/, ""); // some decoding is probably necessary
                 var cookieValue = keyValuePair.replace(/^[^=]*\=/, ""); // some decoding is probably necessary
                 COOKIES[cookieName] = cookieValue;
             });
             document.getElementById("myForm").getElementsByTagName("input")[0].value = COOKIES["form/title"];
             document.getElementById("myForm").getElementsByTagName("textarea")[0].value = COOKIES["form/text"];
         </script>
     </body>
    </html>
    

    Example: Using the Query String

    In the case of using the Query String, the first page would just include the query string in the redirect URL, like so:

    <!DOCTYPE html>
    <html>
     <head>
         <title>Maintaining State With The Query String</title>
     </head>
     <body>
         <div>
             Redirecting...
         </div>
         <script>
             setTimeout(function(){
                 window.location = "./form-querystring.html?form/title=My Name is Richard&form/text=I am demoing how to use the query string in JavaScript";
             }, 1000);
         </script>
     </body>
    </html>
    

    …while the form would then parse the query string (available in JavaScript via window.location.search – prepended with a ?):

    <!DOCTYPE html>
    <html>
     <head>
         <title>Maintaining State With The Query String</title>
     </head>
     <body>
         <form id="myForm" action="submit.mumps.cgi" method="POST">
             <input type="text" name="title" />
             <textarea name="text"></textarea>
         </form>
         <script>
             var GET = {};
             var queryString = window.location.search.replace(/^\?/, '');
             queryString.split(/\&/).forEach(function(keyValuePair) {
                 var paramName = keyValuePair.replace(/=.*$/, ""); // some decoding is probably necessary
                 var paramValue = keyValuePair.replace(/^[^=]*\=/, ""); // some decoding is probably necessary
                 GET[paramName] = paramValue;
             });
             document.getElementById("myForm").getElementsByTagName("input")[0].value = GET["form/title"];
             document.getElementById("myForm").getElementsByTagName("textarea")[0].value = GET["form/text"];
         </script>
     </body>
    </html>
    

    Example: With a Fragment Identifier

    There’s one more option: since state is being maintained strictly on the client side (not on th server side) you could put the information in a fragment identifier (the “hash” part of a URL).

    The first script is very similar to the Query String example above: the redirect URL just includes the fragment identifier. I’m going to re-use query string formatting for convenience, but notice the # in the place where a ? used to be:

    <!DOCTYPE html>
    <html>
     <head>
         <title>Maintaining State With The Fragment Identifier</title>
     </head>
     <body>
         <div>
             Redirecting...
         </div>
         <script>
             setTimeout(function(){
                 window.location = "./form-fragmentidentifier.html#form/title=My Name is Richard&form/text=I am demoing how to use the fragment identifier in JavaScript";
             }, 1000);
         </script>
     </body>
    </html>
    

    … and then the form has to parse the fragment identifier etc:

    <!DOCTYPE html>
    <html>
     <head>
         <title>Maintaining State With The Fragment Identifier</title>
     </head>
     <body>
         <form id="myForm" action="submit.mumps.cgi" method="POST">
             <input type="text" name="title" />
             <textarea name="text"></textarea>
         </form>
         <script>
             var HASH = {};
             var hashString = window.location.hash.replace(/^#/, '');
             hashString.split(/\&/).forEach(function(keyValuePair) {
                 var paramName = keyValuePair.replace(/=.*$/, ""); // some decoding is probably necessary
                 var paramValue = keyValuePair.replace(/^[^=]*\=/, ""); // some decoding is probably necessary
                 HASH[paramName] = paramValue;
             });
             document.getElementById("myForm").getElementsByTagName("input")[0].value = HASH["form/title"];
             document.getElementById("myForm").getElementsByTagName("textarea")[0].value = HASH["form/text"];
         </script>
     </body>
    </html>
    

    And if you can’t edit the code for the form page

    Try a greasemonkey script.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to fill-out a form automatically and press a button on that form
I am trying to fill object[] with List<string> but I cannot figure out how
I'm am trying to write vba code to fill out web-forms and click buttons
I've been trawling books online and google incantations trying to find out what fill
I'm trying out the HighStock library for creating stock charts. To fill the chart
I'm trying to fill my ListView using AsyncTask. Included problem in the comment private
All, I'm trying to have someone fill out some information on my website and
On my site users fill out a form to register, this info then gets
I've got a form. In this form, a user can fill out a few
I have a form that a user can fill out that maps to an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.