jquery - Overwrite function javascript in child window? -
i open window : var popup =window.open(url,'image', 'directories=no,height=640,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,width=680');
in child window opened have function openfile(url); can overwrite function in parent window? or bind click event element in child window parent window (because can't edit child page). parent , child window in same domain.
here how make it:
var popup =window.open(url,'image', 'directories=no,height=640,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no') function waitonloadwindow(){ var body = popup.document.getelementsbytagname("body"); // not loaded yet if(!body[0] || !popup.openfile){ settimeout(waitonloadwindow, 50); }else{ // replace child window function here popup.openfile = function(url){ alert('replaced');} } } waitonloadwindow();
a more generic way (if have more 1 function replace) :
function replacewindowfunction(win, fnname, fnfunction) { var body = popup.document.getelementsbytagname("body"); if(!body[0] || !popup[fnname]){ // wait loading settimeout(function() { replacewindowfunction(win, fnname, fnfunction); }, 50); }else{ // replace child window function here popup[fnname] = fnfunction; } } // usage: var popup = window.open("test.html",'image', 'directories=no,height=640,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no') replacewindowfunction(popup, 'openfile', function() { alert('replaced'); } );
Comments
Post a Comment