2012-08-29 29 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

    相關問題