【发布时间】:2013-02-02 17:40:46
【问题描述】:
我想发布表单值并使用 Javascript 在另一个 html 页面上显示它们。不应使用服务器端技术。我有一个发布值的函数,但要将值读取到另一个 html 页面,我想我遗漏了一些东西。下面是代码。 有什么帮助吗?提前致谢。
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<script type="text/javascript">
function post_to_page(path, params, method) {
method = method || "post"; // Set method to post by default, if not specified.
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);
form.setAttribute("target", "formresult");
for (var key in params) {
if (params.hasOwnProperty(key)) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);
form.appendChild(hiddenField);
}
}
document.body.appendChild(form);
// creating the 'formresult' window with custom features prior to submitting the form
window.open('target.htm', 'formresult', 'scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,status=no');
form.submit();
}
</script>
</head>
<body>
<form id="form1" action="target.htm" method="post">
<div>
USB No: <input name="usbnum" id="usbnum" type="text"/><br />
USB Code: <input name="usbcode" id="usbcode" type="text"/>
</div>
<button onclick="post_to_page()">Try it</button>
</div>
</form>
</body>
</html>
【问题讨论】:
-
也许有帮助:
window.postMessage。由于您没有向服务器提交任何数据,因此您实际上并不想 POST 而是将信息从一个窗口传递到另一个窗口。由于您可能传递相同的源策略,因此您可以访问另一个窗口的 Window(从window.open调用返回),因此有很多方法可以传递信息。
标签: javascript html forms post