1

Basically, I have a page in which I include a javascript file. in the js file i have 2 functions called function a() and function b(). Inside Function "A" I called the popup to be loaded. After the child popup is loaded I have a function called interactive which makes an ajax call. if the result is true I need to close the child page and then call the function "B" in the main page.Below is the sample code. The problem is I am not able the refer the function "B" in the child page and make a call to the main page.

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);
    }
});
George
  • 6,630
  • 2
  • 29
  • 36
jai ganesh
  • 87
  • 1
  • 9
  • So you´r open a new window? – Martin Godzina May 03 '17 at 10:45
  • yes. in child window before closing the popup i need to call the function in the main window – jai ganesh May 03 '17 at 10:49
  • add an `onbeforeunload` eventhandler for the childwindow that calls the reload function – Icepickle May 03 '17 at 10:50
  • Check this question: http://stackoverflow.com/questions/25775862/return-a-value-from-window-open, especially the "second idea" of the accepted answer. – nonzaprej May 03 '17 at 10:51
  • Go to parent window and then call the function. This will work only if your parent and child are in same doman. Unless you have to use postmessages. Use `window.parent.b()` if both are in same domain. – Shubham May 03 '17 at 10:52

2 Answers2

0

You can do it in such a way that the window you open receives an onbeforeunload event that calls your reload function in the parent window.

An example would be the following

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;
  });
}

Which you can test on jsfiddle here (window.open is not allowed in the stacksnippets)

Icepickle
  • 12,689
  • 3
  • 34
  • 48
  • @jaiganesh Could you be more specific on the IE version, for me in Chrome, Firefox and internet explorer the example works (where IE shows me a confirmation dialog, however it updates the reloaded part) – Icepickle May 03 '17 at 13:04
  • ie version more than 11 – jai ganesh May 03 '17 at 13:20
  • @jaiganesh And your emulation mode is not set to an intranet site, is it? You are making it hard to provide you good feedback with such little information you are willing to give – Icepickle May 03 '17 at 13:28
  • @jaiganesh The basic browser support for [addEventListener](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Browser_compatibility) is IE 9 as you can see from the link – Icepickle May 03 '17 at 13:49
-1

I think you need to add function b() in child page so it's easily access,So please check with this.

Hitesh Gohel
  • 117
  • 4