如何从窗口外调用函数

时间:2016-07-08 05:34:51

标签: javascript jquery html

如何调用在父窗口中声明的函数? 我有一个通过调用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")
}

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

父母应该参考开场窗口。

parent.method()会调用方法。它必须在同一个域上,没有跨域调用可以工作。

答案 1 :(得分:1)

使用windiw.opener helper调用父窗口函数:

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

答案 2 :(得分:0)

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

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

和父母:

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

或尝试这种方法:Possible to call a Javascript method in the context of another window?