2012-10-04 51 views
0

在這裏工作是我的代碼:window.focus()的在Chrome

var myWindow = window.open('','zzz','width=600,height=700'); 
myWindow.document.write('test'); 
myWindow.blur(); 
myWindow.focus(); 
window.onfocus=function(){ 
    this.document.write('<br />Focused!'); 
} 
var w = window; 
myWindow.onfocus=function(){ 
    this.blur(); 
    this.document.write('<br />Focusing on main window!'); 
    w.focus(); 
}; 
myWindow.focus(); 

這裏是小提琴:http://jsfiddle.net/maniator/nMGNS/

當彈出窗口集中它應該在彈出的打印Focusing on main window!然後專注於主窗口並在那裏打印Focused!

但它沒有。

這裏是關於我的瀏覽器的信息:

Google Chrome 22.0.1229.79 (Official Build 158531) m 
OS Windows 
WebKit 537.4 (@129177) 
JavaScript V8 3.12.19.11 
Flash 11.3.31.331 
User Agent Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4 

除了

我瞭解alert(...)劈,我想避免它。

+3

頁面加載後,您不能使用'document.write'。如果你想測試,使用瀏覽器的控制檯。例如'console.log(「here」);' – epascarello

+1

@epascarello這只是一個簡單的例子。兩者都會工作。而且 - 當然,我可以在頁面加載後執行'document.write' ...它將工作得很好.... – Neal

+0

頁面加載後的document.write替換頁面內容,它將擦除什麼。你可以做到,但它會通過覆蓋它來破壞頁面! – epascarello

回答

0

通過@ epascarello的評論,改變document.writealert,它可以工作鉻:

var myWindow = window.open('','zzz','width=600,height=700'); 
myWindow.document.write('test'); 
myWindow.blur(); 
window.onfocus=function(){ 
    alert('Focused!'); 
} 
var w = window; 
myWindow.onfocus=function(){ 
    this.blur(); 
    alert('Focusing on main window!'); 
    w.focus(); 
}; 
myWindow.focus(); 
+0

我知道abt警報破解。我試圖避免阻止那樣的窗口... – Neal

+0

此外,這似乎並沒有工作。我只是試圖在一個小提琴無濟於事:http://jsfiddle.net/maniator/nMGNS/3/ – Neal

+0

我認爲這不能實現在鉻,連續的焦點將被屏蔽在鉻 – Koerr