繁体   English   中英

如何从窗口外部调用函数

[英]How to call a function from outside the window

如何调用在父窗口中声明的函数? 我有一个通过调用window.open打开的新窗口,新窗口的内容是一个网格,网格中的每个项目都有链接/锚点。 我想在每个项目的onclick事件中调用在父html上声明的函数。 我该如何实现? 以下是我的示例代码。

function showCustomWindow(filter){

    var title = '';
         source = behindDueDates;
         title = 'Client Actions Behind Due Dates';

    var htmlContent = '<div id="content"><h2> '+ title +'</h2><table class="table" id="behindActions">';
        htmlContent +='<thead class="thead-inverse">';
        htmlContent += '<tr class="tableHeader">';
        htmlContent += '<td> Subject </td>';
        htmlContent += '<td> Regarding </td>';
        htmlContent += '</tr>';
        htmlContent += '</thead>';
        htmlContent += '<tbody>';

        $.each(source, function(key, value){
                var regarding = value.attributes.regardingobjectid;

                htmlContent += '<tr>';
                htmlContent += '<td data-id="'+value.Id+'"><a href="#" onclick="redirectToRecord("'+value.Id + "entity"+ value.attributes.activitytypecode.formattedValue +'")">' + value.getValue("subject") + '</a></td>';
                htmlContent += '<td>' + regarding + '</td>';
                htmlContent += '</tr>';
        })                

        htmlContent += '</tbody>';
        htmlContent += '</table></div>';

    var win = window.open("", title, "location=1,status=1,scrollbars=1,width=1000,height=800");
        win.moveTo(10, 10);
        win.document.write('<html><head><title> ' + title + ' </title></head><body>');
        win.document.write(htmlContent);
        win.document.write('</body></html>');

}
function redirectToRecord(value){
   alert("Event to call from second window")
}

任何帮助深表感谢。

家长应参考打开的窗口。

parent.method()将调用一个方法。 但是它必须位于同一域上,没有跨域调用将起作用。

使用windiw.opener帮助器调用父窗口函数:

if (window.opener != null && !window.opener.closed) {
    window.opener.function_in_parent(); //Parent window function
}

您可以通过以下方式访问子窗口:

var w = window.open("/uri", "title");
w.childFunction();

和父母:

<a onclick="parent.anyname();" href="#" > Click me </a>

或尝试以下方法:是否可以在另一个窗口的上下文中调用Javascript方法?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM