我只是在此處引用文檔,僅供需要參考的任何人點擊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