如何使用JavaScript在另一页上填写表单

如何使用JavaScript在另一页上填写表单,javascript,forms,Javascript,Forms,我试图通过JavaScript填写表单上的字段。问题是我只知道如何在当前页面上执行JavaScript,因此无法重定向到表单并从那里执行代码。我不太愿意使用这个术语,但我想到的唯一短语是跨站点脚本。下面是我试图执行的代码 <script language="javascript"> window.location = "http://www.pagewithaform.com"; loaded(); //checks to see if page is loaded. if

我试图通过JavaScript填写表单上的字段。问题是我只知道如何在当前页面上执行JavaScript,因此无法重定向到表单并从那里执行代码。我不太愿意使用这个术语,但我想到的唯一短语是跨站点脚本。下面是我试图执行的代码

<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>

window.location=”http://www.pagewithaform.com";
加载();
//检查页面是否已加载。如果不是,则在超时后检查。
函数加载()
{
if(窗口加载)
{
//从不在新页面上执行。问题是
setTitle();
}
其他的
{
setTimeout(“loaded()”,1000);
警报(“新警报”);
}
}
//设置字段的值
函数setTitle()
{
变量标题=提示(“字段信息”、“默认值”);
var form=document.form[0];
form.elements[“fieldName”]。value=标题;
}
我不确定这是否可行。我也对其他想法持开放态度,比如PHP。谢谢


编辑:第二个页面是SharePoint表单。我无法编辑表单上的任何代码。我们的目标是编写一个预填充大多数字段的脚本,因为其中90%是静态的。

这是一个很好的使用方法

例:从

函数createCookie(名称、值、天数){
如果(天){
变量日期=新日期();
date.setTime(date.getTime()+(天*24*60*60*1000));
var expires=“;expires=“+date.togmString();
}
else var expires=“”;
document.cookie=name+“=”+value+expires+“path=/”;
}
函数readCookie(名称){
变量nameEQ=name+“=”;
var ca=document.cookie.split(“;”);
对于(变量i=0;i
另外,您可以使用onload事件来知道页面何时准备就绪

<script language="javascript"> 

function setTitle(){
    var title = prompt("Field Info","Default Value");
    var form = document.form[0];
    form.elements["fieldName"].value = title;
}

windows.onload = setTitle;

</script>

函数setTitle(){
变量标题=提示(“字段信息”、“默认值”);
var form=document.form[0];
form.elements[“fieldName”]。value=标题;
}
windows.onload=setTitle;

您正在尝试维护页面之间的状态。通常有两种方式来维持状态:

  • 在cookies中存储状态
  • 在查询字符串中存储状态
无论哪种方式,您的第一个页面都必须保持状态(cookies或查询字符串),而另一个页面必须单独恢复状态。不能在两个页面上使用相同的脚本

示例:使用Cookies 使用cookies,第一页必须将下一页所需的所有表单数据写入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>
示例:使用片段标识符 还有一个选项:由于状态严格地在客户端(而不是服务器端)维护,所以可以将信息放在片段标识符(URL的“哈希”部分)中

第一个脚本与上面的查询字符串示例非常相似:重定向URL只包含片段标识符。为了方便起见,我将重复使用查询字符串格式,但请注意
#
中的
过去是:

<!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>

使用片段标识符维护状态
重定向。。。
setTimeout(函数(){
window.location=“./formfragmentidentifier.html#form/title=我的名字是Richard&form/text=我正在演示如何在JavaScript中使用片段标识符”;
}, 1000);
。。。然后表单必须解析片段标识符等:

<!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>

使用片段标识符维护状态
var HASH={};
var hashString=window.location.hash.replace(/^#/,“”);
hashString.split(/\&/).forEach(函数(keyValuePair){
var paramName=keyValuePair.replace(//=.*$/,“”);//可能需要一些解码
var paramValue=keyValuePair.replace(//^[^=]*\=/,“”);//可能需要一些解码
HASH[paramName]=paramValue;
});
document.getElementById(“myForm”).getElementsByTagName(“input”)[0]。value=HASH[“form/title”];
document.getElementById(“myForm”).getElementsByTagName(“textarea”)[0]。value=HASH[“form/text”];
如果无法编辑表单页面的代码

如果可以在不访问目标系统/源代码/mitm方法的情况下操纵目标网站,那么这将是恶意软件与点击劫持相结合的一条开放的高速公路!我不想让你的脚本告诉我的银行该怎么做


为此,请使用AutoIt(www.autoitscript.com)等自动化工具。易于学习,具有良好的形式整合性。如果标准还不够,请为AutoIt寻找winhttp之类的UDF。

使用本地存储非常容易。
第一页表单


输入{
字体大小:25px;
}
标签{
颜色:rgb(16,8,46);
字体大小:粗体;
}
#资料{
}
函数getData()
{
//获取值
var email=document.getElementById(“email”).value;
var password=document.getElementById(“密码”).value;
var电话=document.getElementById(“电话”).value;
var mobile=document.getElementById(“mobile”).value;
//将值保存在本地存储中
setItem(“txtValue”,电子邮件);
setItem(“txtValue1”,密码);
setItem(“txtValue2”,移动设备);
setItem(“txtValue3”,电话);
}
报名表
电子邮件:

密码

手机:

电话:

这是第二页


//显示从本地存储到另一页的值
<!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>
<!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>
<!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>
<!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>