<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>window.open()方法以post请求方式打开新窗口</title>
</head>
<body>
<button type="button" οnclick="open()">打开</button>
<script src="jquery-1.10.2.js"></script>
<script>
function open() {
var url = "";
// 请求参数 postData
var postData = {};
postData.param1 = "param1";
postData.param2 = "param2";
openNewWindow(url,postData);
}
/**
*
* @param url 请求地址
* @param postData 请求参数
*/
function openNewWindow(url,postData) {
var winWidth = $(window).width() * 0.8;
var winHeight = $(window).height() * 0.9;
var winLeft = $(window).width() * 0.1;
var newWin = window.open("about:blank","_blank","width="+ winWidth +",height="+ winHeight +",top=10,left="+ winLeft +",menubar=no,resizable=no,location=no,status=no");
// 组装form表单
var html = "<form action='"+ url +"' method='post'>";
jQuery.each(postData, function (i,obj) {
html += "<input type='text' name='"+ i +"' value='"+ obj +"' style='display: none'>";
});
html += "</form>";
newWin.document.body.innerHTML = html;
newWin.document.forms[0].submit();
// 监听窗口关闭事件
var loop = setInterval(function () {
if(newWin.closed) {
clearInterval(loop);
}
}, 500);
}
</script>
</body>
</html>
window.open()方法以post请求方式打开新窗口
最新推荐文章于 2024-06-29 12:13:13 发布