如何打开新选项卡并更改当前页面

时间:2008-11-06 12:17:02

标签: javascript

<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.com/" >
</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>

是否可以打开新的标签/窗口并更改当前页面?

4 个答案:

答案 0 :(得分:1)

据我所知,一次提交两张表格是不可能的。既然您正在使用PHP,为什么不看一下cURL库?它允许您发送POST和GET请求,并在PHP中解析结果。

要回答标题中的问题,如果您只是想通过一次点击打开两个页面,就可以这样做(请原谅内联javascript):

<a
    href="http://www.google.com"
    target="_blank"
    onclick="document.location.href='http://www.yahoo.com'"
>Click here to open Google in a new window and yahoo in this window</a>

答案 1 :(得分:1)

<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.com/" >
</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保留为目标url的源,可以是数组或属性。 没有setyTimeout()浏览器停留在当前页面(FF / IE)。

答案 2 :(得分:0)

您应该使用window.open打开新页面,然后提交tabchange,例如

<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.com/", "_blank");
        document.getElementById("tgt_"+id).submit();
    }
</script>

答案 3 :(得分:0)

使用表单元素上的'target'属性将它们提交给例如。新窗口或iframe,无需重新加载当前页面(打破任何其他表单提交)。

您的a.onclick也应返回false以停止跟踪href链接。真的,这应该是一个按钮而不是链接。

除非你有充分的理由,否则请避免使用这类东西。根据您实际尝试的方式,通常有更好的方法。