2017-05-03 112 views
1

基本上,我有一個頁面,其中包含一個javascript文件。在js文件 我有兩個功能稱爲功能a()和功能b()。內函數「A」我調用彈出來加載。加載子彈出後,我有一個稱爲交互的函數,它會調用ajax。如果結果爲真,我需要關閉子頁面,然後在主頁面中調用函數「B」。下面是示例代碼。問題是我不能在子頁面中引用函數「B」並調用主頁面。關閉子頁後刷新主頁

function a() { 
    var strWindowFeatures = "location=yes,scrollbars=yes,status=yes"; 
    var URL = path of the url 
    var win = window.open(URL, "_blank", strWindowFeatures); 
} 

function b() { 
    calling a 
    function to relaod the page. // 
} 


function InterActive(encodeText) { 

    url: url 
    cache: false, 
    async: false, 
    type: 'GET', 
    contentType: 'application/json; charset=utf-8', 
    data: {}, 
    dataType: 'json', 
    success: function (data) { 
     if (data === true) { 
      alert('record updated'); 
      window.close(); 
      // i need to call the function b() in the child page.. 

     } 
    }, 
    error: function (jqXHR, textStatus, errorThrown) { 
     alert(jqXHR); 
     alert(textStatus); 
     alert(errorThrown); 
    } 
}); 
+0

所以你打開一個新窗口? –

+0

是的。在關閉彈出窗口之前的子窗口中,我需要調用主窗口中的函數 –

+0

爲子窗口添加一個'onbeforeunload'事件處理程序,用於調用重載功能 – Icepickle

回答

0

您可以在打開的窗口接收到一個onbeforeunload事件調用父窗口的裝載功能,這樣的方式做到這一點。

一個例子是以下

function reloader() { 
    if (!reloader.id) { 
    reloader.id = 0; 
    } 
    document.querySelector('#reloadCount').innerHTML = ++reloader.id; 
} 

function openWindow() { 
    var win = window.open('about:blank', 'blank', 'width=640,height=480'); 
    win.document.write('<html><head><body><h1>Reloads on close</h1></body></html>'); 
    win.addEventListener('beforeunload', function() { 
    reloader(); 
    return true; 
    }); 
} 

,你可以在jsfiddle here測試(window.open不允許在stacksnippets)

+0

不可以。主窗口函數沒有被調用 –

+0

addEventListener在IE中出錯 –

+0

@jaiganesh你可以更具體的IE版本,對於我在Chrome,Firefox和Internet Explorer中的示例工程(IE顯示給我一個確認對話框,但是它更新重新加載的部分) – Icepickle

-1

我認爲你需要在孩子添加函數B()頁面,所以很容易訪問,所以請檢查這一點。

+0

這對OP有何幫助? – Icepickle