我的網頁和IFrame一起倒計時。倒計時將在$(window).blur
停止,並從$(window).focus
開始。當我點擊IFrame時,它會觸發模糊事件並停止倒計時。我通過以下代碼行解決了這個問題。我的網頁內的IFrame觸發模糊事件
$(window).blur(function() {
if (document.activeElement.nodeName == "IFRAME") {
//Continue timer
else {
//Stop timer
}
});
但是,當我單擊內部IFrame後單擊窗口外部時,模糊事件不會被觸發。由於模糊事件將不會在模糊事件之後觸發,除非觸發焦點事件。
我剛纔提到類似的主題 - $(window).blur event affecting Iframe和 How can I capture the blur and focus of the entire browser window?
任何其他的想法?
更新 - 我做了另一種方法,讓我領先一步。以下代碼是新方法
$(window).on('focus', function() {
//Coundown runs
}).on('blur', function() {
//Coundown stops
});;
$(document).ready(function() {
var iframe = document.getElementById("iframeID");
$(iframe.contentWindow).on('focus', function(){
//Coundown runs
}).on('blur', function() {
//Coundown stops
});
});
此代碼將使iframe焦點上的倒計時運行並停止iframe模糊。
但是,我在這種方法面臨的問題是倒計時工作正常時,「scr」不加載iframe。如果在iframe中加載了「scr」,則Iframe的焦點和模糊事件不會在Iframe點擊時變爲綁定和倒計時。
對此有何想法?
@ user3656699-感謝您的想法。我已經通過鏈接和postmessage主題。只有當父和iframe的內容都由我自己擁有時,才能使用postmessage? (參考[鏈接](http://stackoverflow.com/questions/25098021/securityerror-blocked-a-frame-with-origin-from-accessing-a-cross-origin-frame))因爲我面臨的問題 - **用交叉起點**封鎖了一個框架。 –
感謝您的postmessage想法。它爲我工作。但我必須在瀏覽器中禁用跨源策略。因爲這兩個網站並非由我自己和不同來源。 –
@AswinMR是的,如果iframe的內容不是你所擁有的,那麼你無法看到那裏正在發生什麼,並且postmessage將需要在iframe中執行一部分。 (例如,考慮iframe加載隨機頁面的情況,並且包裝頁面可能會使用瀏覽器自動填充的用戶名和密碼信息) –