且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

关闭按钮上的窗口或选项卡

更新时间:2023-02-03 23:34:22

您可以使用 window.open()来创建新窗口,并控制何时关闭它们.

You can use window.open() to create new windows, and to control when to close them.

示例:

function mypopup() {
    mywindow = window.open ("http://www.javascript-coder.com",
                            "mywindow",
                            "location=1,status=1,scrollbars=1,"+
                            "width=100,height=100");
    mywindow.moveTo(0,0); // move the window to a particular location

    // do your stuff ...

    mywindow.close(); // close that popup when you are done
}

如您所见,您可以使用 window.close().

As you can see, you can control when you want to close the windows using window.close().

您只需在链接中使用target="_blank"即可在新选项卡中打开链接,但是您说您有一个表单,其提交将导致打开新窗口.但是,与其使用提交按钮,不如使用普通按钮并将事件处理程序附加到被单击的事件处理程序上.单击它后,打开窗口并使用您的mojo.

You can have links open in new tabs simply by using target="_blank" in the link, but you say that you have a form whose submission will cause a new window to be opened. But instead of using a submit button, use a plain button and attach an event handler to it being clicked. When it is clicked, open your window and work your mojo.

关于您关于打开选项卡而不是窗口的查询,我不确定是否可以完成.我可以想到使用服务器端参与的几种不良方法,但是我敢肯定您不会在寻找这种方法.甚至使用target="_blank"只是告诉浏览器不要在同一页面中打开链接.由浏览器决定是要选择一个新窗口还是一个新选项卡(通常是一个新选项卡).

Regarding your query about opening tabs instead of windows, I'm not entirely sure it can be done. I can think of a couple of bad ways to do it using server side participation, but I'm pretty sure you are not looking for that. Even using target="_blank" just tells the browser not to open the link in the same page. Its upto the browser to choose a new window or a new tab (mostly, its a new tab).

干杯!