我只是在此处引用文档,仅供需要参考的任何人点击W3C和MDN。
function openWin() {
myWindow = window.open("", "myWindow", "width=200, height=100"); // Opens a new window
}
function closeWin() {
myWindow.close(); // Closes the new window
}
进行分解,在open
和close
函数使用比参数可以是非常有用的,比如URL
,希望打开或关闭当前窗口的时候,或者在你的情况打开窗口。
一个实际的例子是this堆栈溢出问题。 N
我希望它有帮助!
编辑
要回答OP的编辑的问题:如果是触发其打开的窗口上的一个事件的问题,你可以在新窗口中有一个事件处理程序,这将触发window.close()
像这样:
$('#anElementId').click(function() { window.opener.$('body').trigger('theCloseEvent', anyPassedData); })
但是,如果你真的是有在新的标签控制,因为它会导致它的代码库,你是在控制的URL,那么它只是触发事件的问题那里你可以触发该窗口加载一次,或一旦你点击一个按钮...像这样:
HTML
<button id="close-window">Close me</button>
的Javascript/jQuery的:
$(document).ready(function(){
$("#close-window").click(function(){
alert("ok");
window.close();
});
});
编辑#2
要进一步扩展OP的编辑,我想在这里包含一个问题,当试图触发打开的窗口关闭自己时很容易遇到。
从How can I close a browser window without receiving the “Do you want to close this window” prompt?报价:
Scripts are not allowed to close a window that a user opened. This is considered a security risk. Though it isn't in any standard, all browser vendors follow this (Mozilla docs). If this happens in some browsers, it's a security bug that (ideally) gets patched very quickly.
None of the hacks in the answers on this question work any longer, and if someone would come up with another dirty hack, eventually it will stop working as well.
I suggest you don't waste energy fighting this and embrace the method that the browser so helpfully gives you — ask the user before you seemingly crash their page.
换句话说,除非你的网页的脚本有,就是打开窗口的控制,你应该不能说接近/窗。这是因为运行window.close
的脚本无法控制打开的窗口。
编辑#3
所以很多编辑我知道了!但我在日常工作之间回答这个问题,所以忍耐着我。要回答porthole.js的问题,应该有更多的可能性去做,但是你需要意识到你正在使用iframe。
有一个显著差异与网站工作VS与I帧,在iframe会Widget和网站(包括微型站点)工作时给出一个网址。在安全性和沙箱方面也有很多考虑因素,可以在portholejs demo here上看到。这种差异也是不允许你以最初的方式使用不同网站的方式。
我的建议是相对于您的实现,以评估你的选择:网站,网站VS网站,窗口小部件。
祝你好运!
你可以在窗口加载时调用javascript window.close()按钮点击 – AGE
我很确定用'window.open()'打开的窗口可以用'window.close()'关闭。 –
[如何关闭浏览器窗口中的当前选项卡?](http://stackoverflow.com/questions/2076299/how-to-close-current-tab-in-a-browser-window) – LGSon