以post方式传参方式打开新窗口

在使用winow.open打开新窗口时,因为是get方式传参,经常因为参数过长产生一些错误,考虑改用post传参来避免这一问题。
要改为post传参方式需要使用一个隐藏form来提交参数,但使用form的target新建窗口又不能自定义window样式。
google了下,可以在提交form的时候使用window.open新建一个空窗口,并将form的target设置为window的name,就能使form的新建的窗口内提交了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* 使用post打开新页面 */
function openPostWindow(url, args, name){
	var tempForm = document.createElement("form");
	tempForm.id="tempForm";
	tempForm.method="post";
	tempForm.action=url;
	tempForm.target=name;
	tempForm.style.display="none";
        //可传入多个参数
	for(var i=0; i<args.length; i++){
		var hideInput = document.createElement("input");
		hideInput.type="hidden";
		hideInput.name=args[i][0];
		hideInput.value=args[i][1];
		tempForm.appendChild(hideInput);  
	}
	tempForm.attachEvent("onsubmit",function(){ window.open("about:blank",name,"directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no"); });
	document.body.appendChild(tempForm);
	tempForm.fireEvent("onsubmit");
	tempForm.submit();
	document.body.removeChild(tempForm);
}

发表评论

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

分类目录