wac*_*cha 4 javascript forms post firefox-addon
我正在尝试使用纯粹的Javascript在Firefox扩展中创建和发送表单.但是,即使使用最基本的表单,浏览器控制台也会说:
TypeError:form.submit不是函数
我搜索并阅读了几个关于这个主题的帖子,但他们没有处理这个特殊问题.一个常见的错误是有一个名为"submit"的输入,但这不是这种情况.
这是我的代码,灵感来自这个伟大的后期JavaScript帖子请求,如表单提交:
// Create a form
var form = document.createElement("form");
form.action = "http://example.com";
form.method = "POST"
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "submit");
hiddenField.setAttribute("name", "sub");
hiddenField.setAttribute("value", "send");
form.appendChild(hiddenField);
// Create a document
var doc = document.implementation.createDocument ('http://www.w3.org/1999/xhtml', 'html', null);
var body = document.createElementNS('http://www.w3.org/1999/xhtml', 'body');
body.appendChild(form);
doc.documentElement.appendChild(body);
// Call the form's submit method
form.submit();
Run Code Online (Sandbox Code Playgroud)
我是Javascript的新手,所以很可能我犯了一个小错误.谁能给我一个如何解决这个问题的线索?
当你创建一个新的元素"形式",你是在浏览器的范围内创造它,因此你要创建一个XUL元素.您必须以创建"body"元素的方式执行此操作:
var form = document.createElementNS('http://www.w3.org/1999/xhtml', 'form');
Run Code Online (Sandbox Code Playgroud)