2012-08-29 27 views
4

我想通过JavaScript填写表单上的字段。问题是我只知道如何在当前页面上执行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> 

我不确定这是否可能。我也接受其他想法,比如PHP。谢谢。

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

+0

你为什么不使用Cookie或本地存储,或存储flash范围/会话中的值以及下一页中的值 – Ankur

+0

我无权访问下一页。这是SharePoint上的一种表格,我厌倦了重复填写。大部分字段每次都是相同的,所以我希望能够用脚本来填充这些字段。有没有办法做你提到的任何事情? – spassen

+0

你不能这样做,直到你有一些可用的Sharepoint的API,因为如果它是会导致严重的安全问题 – Ankur

回答

13

你正试图维持页面之间的状态。传统上有两种方式,以保持状态:在曲奇

  • 存储状态
  • 查询字符串

无论哪种方式,您的第一页必须坚持国家

  • 存储状态(要么饼干或查询字符串),另一页必须 - 单独 - 恢复状态。您无法在两个页面中使用相同的脚本。

    例如:使用Cookie

    使用Cookie,第一页会写所有你需要的下一个页面上的cookie表单数据:

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

    ...和第二那么网页将读取这些cookie和填充他们的表单字段:

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

    示例:使用查询字符串

    在使用查询字符串的情况下,第一页将只包括在重定向URL的查询字符串,像这样:

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

    ...而形式将然后解析查询字符串(可用通过window.location.search JavaScript的 - 有?前置):

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

    实例:利用片段标识符

    还有一个选择:因为国家正在严格保持在客户端上(不在服务器端),你可以把信息放在一个片段标识符(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> 
    

    ...然后形式具有解析片段标识符等:

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

    如果你不能为表单页面编辑代码

    Try a greasemonkey script.

  • 1

    它是使用cookies

    防爆的好去处:从quirksmode.org

    function createCookie(name,value,days) { 
        if (days) { 
         var date = new Date(); 
         date.setTime(date.getTime()+(days*24*60*60*1000)); 
         var expires = "; expires="+date.toGMTString(); 
        } 
        else var expires = ""; 
        document.cookie = name+"="+value+expires+"; path=/"; 
    } 
    
    function readCookie(name) { 
        var nameEQ = name + "="; 
        var ca = document.cookie.split(';'); 
        for(var i=0;i < ca.length;i++) { 
         var c = ca[i]; 
         while (c.charAt(0)==' ') c = c.substring(1,c.length); 
         if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); 
        } 
        return null; 
    } 
    

    和一个侧面说明,您可以使用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> 
    
    +0

    我可能是错的,但我不认为我可以在我的情况下使用Cookie。为了他们的工作,我不需要通过下一页中的代码访问它们吗?我无法访问第二页的源代码。这是一种我厌倦每次填写的SharePoint表单。 90%的字段值是静态的。我希望只用脚本设置值并提示输入标题,这样我就可以在下一页上单击确定。 – spassen

    +0

    @spassen如果你的表单在你自己的服务器上,你可以做你想做的事。该表单可以使用javascript填充 – Ibu

    相关问题