admin管理员组文章数量:1400445
<form id="frm_1" name="frm_1" target="_self" method="GET" action="local_page.php" >
</form>
<form id="tgt_1" name="tgt_1" target="_blank" method="POST" action="/" >
</form>
<a onclick="test(event, '1'); " href="#" >Click Here</a>
<script>
function test(event, id){
document.getElementById("frm_"+id).submit;
document.getElementById("tgt_"+id).submit;
}
</script>
Is it possible to open a new tab/window and change the current page ?
<form id="frm_1" name="frm_1" target="_self" method="GET" action="local_page.php" >
</form>
<form id="tgt_1" name="tgt_1" target="_blank" method="POST" action="http://stackoverflow./" >
</form>
<a onclick="test(event, '1'); " href="#" >Click Here</a>
<script>
function test(event, id){
document.getElementById("frm_"+id).submit;
document.getElementById("tgt_"+id).submit;
}
</script>
Is it possible to open a new tab/window and change the current page ?
Share Improve this question edited Nov 6, 2008 at 12:30 nickf 547k199 gold badges658 silver badges727 bronze badges asked Nov 6, 2008 at 12:17 John GriffithsJohn Griffiths 3,4334 gold badges23 silver badges19 bronze badges 1-
Still trying this end. Usage of
<a>
was to have the browser status bar show the href url. My original code works when the href param ishref="local_page.php"
and onlydocument.getElementById("tgt_"+id).submit;
exists. BUT this means that I am unable to POST to the local page any info about the chosen option. In this case using GET and appending parameters is not an option. – John Griffiths Commented Nov 6, 2008 at 13:19
4 Answers
Reset to default 1As far as I know, it's not possible to submit two forms at once. Since you're using PHP however, why not take a look at the cURL library? It lets you send POST and GET requests and parse the results in PHP.
To answer the question in the title, if you simply want to open two pages with one click, you could do it like this (excuse the inline javascript):
<a
href="http://www.google."
target="_blank"
onclick="document.location.href='http://www.yahoo.'"
>Click here to open Google in a new window and yahoo in this window</a>
<form id="frm_1" name="frm_1" target="_self" method="POST" action="local_page.php" >
<input type="hidden" name="vital_param" value="<?= $something ?>">
</form>
<form id="tgt_1" name="tgt_1" target="_blank" method="POST" action="http://stackoverflow./" >
</form>
<button type="submit" onclick="test(event, '1'); " >Click Here</button>
<script>
function test(event, id){
window.open( document.getElementById("tgt_"+id).action, "_blank");
setTimeout('document.getElementById("frm_'+id+'").submit();', 1000);
return true;
}
</script>
tgt kept as source of target url, could be array or attribute. Without setyTimeout() browser stays on current page (FF/IE).
You should use the window.open to open the new page and then submit the tabchange e.g.
<form id="frm_1" name="frm_1" target="_self" method="GET" action="local_page.php" >
</form>
<a onclick="test(event, '1'); " href="#" >Click Here</a>
<script>
function test(event, id){
window.open("http://stackoverflow./", "_blank");
document.getElementById("tgt_"+id).submit();
}
</script>
Use the ‘target’ attribute on the form elements to make them submit to eg. a new window or an iframe, without reloading the current page (breaking any other form submissions).
Your a.onclick should also return false to stop the href link being followed. Really this should be a button not a link.
Avoid this sort of stuff unless you've got a specific good reason. Depending on what you're actually trying to do there is usually a better way.
本文标签: javascriptHow to open a new tab and change the current pageStack Overflow
版权声明:本文标题:javascript - How to open a new tab and change the current page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744259211a2597629.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论