2014-10-02 25 views
0

我正在研究一个用于greasemonkey的小脚本,它应该从另一个页面提交表单(所以action属性与我想提交的页面不同它)。我想包含脚本的页面类似于www.pennergame.de/overview/,表单元素称为来自www.pennergame.de/stock/foodstuffs/use/的表单动作。HTML/Javascript获取当前浏览器窗口并关闭它超时

当我点击一个输入按钮,表单元素将被提交。 我现在想要的是,该页面不会重定向到/ stock/foodstuffs/use/Page。

所以,我想 1.提交表单元素 2.打开与/概述/链接一个新窗口并关闭当前窗口或停止重定向到/股票/食品/使用/

我尝试这样:

var overviewLink = 'http://www.pennergame.de/overview/'; 
var input_drink = document.createElement("input"); 
input_drink.type = 'submit'; 
input_drink.id = 'drink_Brot'; 
input_drink.value = 'Nüchtern werden (' + anzBrot + ' Brot essen)'; 
form.onsubmit = function(){ window.setTimeout('window.close()', 600); var fenster = window.open(overviewLink, '_blank'); }; 

什么实际发生的是:在表单提交,新的窗口将被打开,但旧的窗口不会关闭

并请,只有JavaScript的解决方案时可能; - )

+0

'window.setTimeout(window.close,600);' – electrophanteau 2014-10-02 12:27:51

回答

0

丹尼斯,我不知道我是否理解你的问题,但让我描述我从你的消息中得到了什么(纠正我错误)。 你想在当前页面上做点什么(就像按下按钮),然后在另一个页面上调用表单提交。 此外,需要防止出现新的窗口。

  1. 您不应该使用Window对象来解决问题。首先是因为不可能完全隐藏一个窗口。并且无法关闭当前窗口,因为只有使用window.open(URL)方法创建的窗口是可关闭的。

  2. 使用打开子页面。这是很容易操作(包括用户隐藏)

  3. 的步骤试试以下脚本(按动按钮),看看这是怎么回事:

<body> 
 
    <button onclick="createFrame();">createFrame</button> 
 
    <button onclick="fillFormOnAnotherPage();">fillFormOnAnotherPage</button> 
 
    <button onclick="submitFormOnAnotherPage();">submitFormOnAnotherPage</button> 
 
    <button onclick="closeFrame();">closeFrame</button> 
 
    <script> 
 
    var frame 
 
     , frameDoc 
 
     , frameForm 
 

 
    function createFrame() 
 
    { 
 
     frame = document.createElement("iframe"); 
 
     frame.src = 'prev.html' 
 
     frame.setAttribute('hidden', ''); // on debug remove this line to see the frame 
 
     document.body.appendChild(frame); 
 
    } 
 

 
    function fillFormOnAnotherPage() 
 
    { 
 
     // document object of the page where we are looking for the form 
 
     frameDoc = frame.contentDocument 
 

 
     // somehow find your form 
 
     frameForm = frameDoc.getElementById('myFormId') 
 

 
     // yes we can access form elements by their names 
 
     var someInputElement = frameForm['myInputName'] 
 

 
     // just put all the data you want to the form elements 
 
     someInputElement.value = "Greetings from another page" 
 
    } 
 

 
    function submitFormOnAnotherPage() 
 
    { 
 
     frameForm.submit(); 
 
    } 
 

 
    function closeFrame() 
 
    { // if you really need a closing with timeout just wrap this function with setTimeout 
 
     document.body.removeChild(frame); 
 
    } 
 

 
    // call this method from console or script to do all the magic 
 
    function doEverything() 
 
    { 
 
     createFrame() 
 
     fillFormOnAnotherPage() 
 
     submitFormOnAnotherPage() 
 
     closeFrame() 
 
    } 
 
    </script> 
 
</body>

这里is preview.html(page open to frame)

<body> 
 
<form id="myFormId"> 
 
    <input value="fancy" name="myInputName"/> 
 
</form> 
 
</body>

+0

P.S.我看到你使用一个字符串作为setTimeout中的第一个参数。这是不安全的旧式(现在不推荐使用)。您可以传递一个匿名函数作为第一个参数,如下所示:setTimeout(function(){console.log(“Hallo”);},800)'' – diziaq 2014-10-03 07:31:30