Javascript 如何将表单提交到新窗口?
我想使用JavaScript动态插入一个表单,然后提交它并在新窗口中打开目标 这是我的密码:Javascript 如何将表单提交到新窗口?,javascript,jquery,Javascript,Jquery,我想使用JavaScript动态插入一个表单,然后提交它并在新窗口中打开目标 这是我的密码: var form = document.createElement("form"); form.setAttribute("method", "post"); form.setAttribute("action", xxx); form.setAttribute("onsubmit", "window.ope
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", xxx);
form.setAttribute("onsubmit", "window.open('about:blank','Popup_Window','scrollbars=yes,width=550,height=520');");
form.setAttribute("target", "Popup_Window");
document.body.appendChild(form);
form.submit();
这段代码有效——它动态插入表单并成功提交表单。但是,表单是在新选项卡中打开的,而我希望它在新窗口中打开。知道怎么回事吗?有没有关于如何解决这个问题的建议?我也对jQuery持开放态度
谢谢
编辑:我不知道为什么人们会将此标记为重复。是的,它与另一个问题在同一个主题上,但答案并不相关——声称重复的问题有一个新的行,它弄乱了代码,我不知道,但我的代码仍然无法工作…
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "http://www.yahoo.com");
function popitup() {
newwindow = window.open('', 'name', 'width=800,height=600');
if (window.focus) {
newwindow.focus()
}
newwindow.document.body.appendChild(form);
newwindow.document.forms[0].submit();
return false;
}
popitup();
据我所知,大多数浏览器和广告拦截器都会阻止这种脚本打开新窗口,因为它是自动运行的。但是,如果您像这样实现它,用户必须单击一个链接:它更有可能在一个新窗口中打开 使用jQuery:
$('.open-popup').click(function(e) {
e.preventDefault();
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "");
document.body.appendChild(form);
form.submit();
window.open(this.href, '_blank', 'width=300,height=200');
});
HTML:
此外,某些浏览器的首选项会自动禁用在新窗口中打开。因此,您可能希望探索其他选项,或者只是要求用户在新选项卡中打开链接。我将此标记为重复,因为我认为您的问题在于如何将表单发送到弹出窗口。以下是如何在不使用
onsubmit
事件的情况下执行此操作:
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", xxx);
function submitToPopup(f) {
var w = window.open('', 'form-target', 'width=600, height=400, any-other-option, ...');
f.target = 'form-target';
f.submit();
};
document.body.appendChild(form);
submitToPopup(form);
因此,您不必使用
onsubmit
事件从那里创建弹出窗口,而是在发送表单之前先创建它,然后再将表单发送给它。如果您想始终保持弹出窗口的“焦点”,可以使用最初发现的技巧。通过快速打开/关闭/打开它,我们确保它的行为始终像新窗口一样,并抓住焦点
<form name=f1 method=post action=test.html>
<input type=text name=name value='123'>
<input type='submit' value='Submit'
onclick="if(window.open('','reuse_win') != null) {
window.open('','reuse_win').close();
}; this.form.target='reuse_win'; return true;">
</form>
也适用于链接
<a href="http://stackoverflow.com" onclick="
if(window.open('','reuse_win') != null) {
window.open('','reuse_win').close();
}" target="reuse_win">Always opens in the same window and FOCUS</a>
出于安全原因,我需要类似的东西。我必须阻止GET方法,只允许POST方法,以便在弹出窗口中显示PDF。我使用了以下代码:
- url:是要显示的url
- mywindow:窗口的名称
- 参数:'宽度=300,高度=200'尺寸
})var windowopen = function(url,mywindow,parameter) { var form = document.createElement('form'); form.setAttribute("method",'POST'); form.setAttribute("action",url); form.autofocus=true; function popupw(f){ var mypopup = window.open('',mywindow,parameter); f.setAttribute("target", mywindow); document.body.appendChild(f); f.submit(); } popupw(form);
url
参数。谢谢bguiz!!。
var windowopen = function(url,mywindow,parameter) {
var form = document.createElement('form');
form.setAttribute("method",'POST');
form.setAttribute("action",url);
form.autofocus=true;
function popupw(f){
var mypopup = window.open('',mywindow,parameter);
f.setAttribute("target", mywindow);
document.body.appendChild(f);
f.submit();
}
popupw(form);