如何用JS打开新窗口,并关闭原来窗口

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用JS打开新窗口,并关闭原来窗口相关的知识,希望对你有一定的参考价值。

做一个触摸屏查询系统,要打开新页面后关闭父界面 百度之functionopenNewWindow() window.open("Index.aspx","","left=30,top=30,toolbar=no,menubar=no,scrollbars=no,resizable=no,status=no,location=no,directories=no,copyhistory=no,height=620,width=820"); window.opener=null; window.open("","_self"); window.close();
这样写的话,由于窗口拦截的原因,会使新窗口打不开,而且原来的窗口也被关闭了
于是再查询找到解决去方法在新页面中onload="window.opener。opener=null; window.opener.close();"
参考技术A 我来把格式怎规范点:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function openNewWindow()
window.open("forwardnewpage.jsp","","left=30,top=30,height=620,width=820,toolbar=no,menubar=no,scrollbars=no,resizable=no,status=no,location=no,directories=no,copyhistory=no");
window.opener=null;
window.open("","_self");
window.close();

</script>
</head>
<body>
<input type="button" value="打开新窗口关闭旧的" onclick="openNewWindow()">
</body>

</html>
新窗口页面代码(如果浏览器允许弹窗可以不要onload这段代码):
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>new page</title>
</head>
<body onload="window.opener.opener=null; window.opener.close();">
<table border="1" cellpadding="1" cellspacing="0" align="center" style="text-align: center;">
<caption>表格标题</caption>
<tr>
<th>标题1</th>
<th>标题2</th>
<th>标题3</th>
</tr>
<tr>
<td>测试</td>
<td>测试</td>
<td>测试</td>
</tr>
<tr>
<td>测试</td>
<td>测试</td>
<td>测试</td>
</tr>
</table>
<br>
</body>
</html>

在winform中,关闭窗口时刷新父窗口(原来打开此窗口的窗口)

如何在关闭窗口时刷新父窗口(原来打开此窗口的窗口,不一定是mdi窗口), 这种事情在b/s里很简单,但在winform里却不那么好办。因为你不能关闭第一个窗口时再打开另一个窗口,如果这样的话新窗口就一起被关闭了。但是正因为这样,我们可以让刷新的动作在关闭子窗口时进行,当然所有的动作是在父窗口中进行的。晕,不知道说明白了没有。
还是看一下例子吧
public partial class Customer : Form
    {
        public Customer()
        {
            InitializeComponent();
        }

        private void customerBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.customerBindingSource.EndEdit();
            this.customerTableAdapter.Update(this.oadepotDataSet.Customer);

        }

        private void Customer_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“oadepotDataSet.Customer”中。您可以根据需要移动或移除它。
            this.customerTableAdapter.Fill(this.oadepotDataSet.Customer);  //请注意这里

        }

        private void SearchBtn_Click(object sender, EventArgs e)
        {
            DataTable dt=customerTableAdapter.GetDataByKey("%"+QueryTextBox.ToString()+"%");
            customerDataGridView.DataSource = dt;
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            //打开新窗口
            Form addForm = new AddCustomer();
            addForm.Owner = this;
            addForm.ShowDialog();
            this.customerTableAdapter.Fill(this.oadepotDataSet.Customer); //关键就在这里
                        
        }

请注意上面的两行红字,问题就在这里。不关子窗口什么事,所有的动作都在父窗口中完成。

以上是关于如何用JS打开新窗口,并关闭原来窗口的主要内容,如果未能解决你的问题,请参考以下文章

如何用JS openWindow方法每次打开一个新窗口

请教一个js问题,关于window打开新窗口,并关闭新窗口。

Swing组件中,如何用一个BUTTON弹出一个新窗口?

WPF 打开新窗口 如何设置 焦点还在原来的窗口 谢谢

JS window.open()打开新窗口、监听新窗口关闭事件

window.open打开新窗口名写入内容 怎么才能保持新窗口原来的内容