简体   繁体   English

使用Javascript在新窗口中提交表单无法在Firefox中使用

[英]Form submission in new window using Javascript not working in Firefox

I've an internet firewall login and to prevent login timeout and keep logged in I want to send POST login request using JavaScript in a new Window every 30 seconds or so. 我已进行互联网防火墙登录,并且为了防止登录超时并保持登录状态,我想每30秒左右在新窗口中使用JavaScript发送一次POST登录请求。

The following function is working in Chrome but not in Firefox. 以下功能在Chrome中有效,但在Firefox中不起作用。 What could be the reason? 可能是什么原因?

function callback(data) //this keeps getting called every 30 secs from Ajax response
{
    if (data && data.login && data.passwd)
    {

        w = w || window.open('', 'formresult', 'scrollbars=no,menubar=no,height=100,width=100,resizable=yes,toolbar=no,status=yes');

        var form = document.createElement("form");
        form.setAttribute("method", "post");
        form.setAttribute("action", 'http://firewall:8090/login.xml');

        form.setAttribute("target", "formresult");

        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("name", "password");
        hiddenField.setAttribute("value", data.passwd); 
        form.appendChild(hiddenField);

        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("name", "username");
        hiddenField.setAttribute("value", data.login);
        form.appendChild(hiddenField);

        w.document.body.appendChild(form);

        form.submit();

    }
} 

if I change 如果我改变

w.document.body.appendChild(form);

to

document.body.appendChild(form);

then Firefox is appending the form in the main window and not in the opened one. 然后Firefox会将表单附加在主窗口中,而不是在打开的窗口中。

EDIT: I see "Error: Permission denied to access property 'document'" in the Firefox console 编辑:我在Firefox控制台中看到“错误:权限被拒绝访问属性'document'”

This is an old question, but the question "Form submission in new window using Javascript not working in Firefox" still has relevance. 这是一个老问题,但是“使用Javascript在Firefox中无法在新窗口中提交表单”问题仍然具有相关性。 In order for the form submit to work properly and open in a new window, you need to do the following: 为了使表单提交工作正常并在新窗口中打开,您需要执行以下操作:

  1. attach the form to the DOM (document.body) 将表单附加到DOM(document.body)
  2. set the target attribute on the form to '_blank' 将表单上的目标属性设置为“ _blank”

     var form = document.createElement('form'); var input = document.createElement('input'); form.target = '_blank'; form.action = data.websiteUrl; form.method = 'post'; input.name = 'username'; input.value = 'myUserName'; document.body.appendChild(form); form.appendChild(input); form.submit(); 

As to pinging the server every 30 seconds.. there has to be a better way to achieve what you are trying to do instead of doing a form post. 至于每30秒对服务器进行一次ping ..必须有一种更好的方法来实现您要执行的操作,而不是执行表单发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM