2016-08-09 47 views
0

你可以在窗口引用上使用JQuery嗎?我嘗試了以下,但沒有運氣。在窗口引用上使用JQuery?

function GetDiPSWindow() { 
    var DiPSURL = "/DiPS/index"; 
    var DiPSWindow = window.open("", "DiPS", "toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=520,height=875"); 
    if (DiPSWindow.location.href === "about:blank") { 
     DiPSWindow.location = DiPSURL; 
    } 
    return DiPSWindow; 
} 

function AddRecipient(field, nameId) { 
    // Get window 
    var win = GetDiPSWindow(); 

    // Attempt 1 
    $(win.document).ready(function() { 
     var input = win.document.getElementById(field + "_Input"); 
     input.value = nameId; 
    }); 

    // Attempt 2 
    $(win).ready(function() { 
     var input = win.document.getElementById(field + "_Input"); 
     input.value = nameId; 
    }); 

    // Attempt 3 
    $(win).load(function() { 
     var input = win.document.getElementById(field + "_Input"); 
     input.value = nameId; 
    }); 
} 

我在犯一個簡單的錯誤嗎?

編輯出於某種原因,win.document.readyState是「完整的」。不知道這是否有所作爲。

我也曾嘗試:

視圖包含:

<script>var CallbackFunction = function() {}; // Placeholder</script> 

的方法:

function AddRecipient(field, nameId) { 
    var DiPSURL = "/DiPS/index"; 

    if (deliveryChannel === undefined) { 
     deliveryChannel = 0; 
    } 

    var DiPSWindow = GetDiPSWindow(); 
    if (DiPSWindow.location.href === "about:blank") { 
     DiPSWindow.location = DiPSURL; 
     DiPSWindow.onload = function() { DiPSWindow.CallbackFunction = AddRecipient(field, nameId) } 
    } else { 
     var input = DiPSWindow.document.getElementById(field + "_Input"); 
     input.value = input.value + nameId; 
     var event = new Event('change'); 
     input.dispatchEvent(event); 
    } 
} 
+0

不完全。內部jquery自動抓取本地'document',而不是你傳入的任何內容.ready。其他方法可能會工作。爲了完整的功能,我建議說窗口加載它自己的jQuery副本。 –

+0

@KevinB說這是不可能的答案,我會接受它大聲笑 –

回答

0

答案是....有點。這取決於你在做什麼。

您可以在父頁面上使用jquery與iframe中的頁面進行交互,但是,任何需要使用iframe的文檔對象的東西可能無法正常工作,因爲jQuery保留了它所包含和使用的文檔的引用它在不同的地方,包括使用文檔準備處理程序時。因此,您無法綁定到iframe的文檔就緒處理程序,但是您可以綁定其他事件處理程序,並且您可以偵聽iframe的加載事件,以瞭解何時與其文檔進行交互是絕對安全的。

雖然只是將jquery包含在iframe中並使用它會更容易。無論如何它應該被緩存,所以這樣做對性能並沒有真正的損害。