2

I have a parent page that opens a child page (IE11).

I want to add some controls to the parent page, that triggers some functions in the child page.

This is the content of the parent page (simplified):

<script>

    let newWindow = window.open('cannon.htm','Picture Display', 'width=780, height=520');

</script>

<button onclick="newWindow.actions.sayHello()">Say</button>

This is the content of the child page (simplified):

let actions;

window.addEventListener('load', function() {

    actions = (function() {

        return {
            sayHello: function() {
                 console.log("hello");
            }
        }

    })();

});

This throws an error. Why? How can I make it work?

THE ERROR: Unable to get property 'sayHello' of undefined or null reference

devamat
  • 2,293
  • 6
  • 27
  • 50

2 Answers2

0

Use a separate script:

cannon.js:

let actions;

window.addEventListener('load', function() {

    actions = (function() {

        return {
            sayHello: function() {
                 console.log("hello");
            }
        }

    })();

});

cannon.htm:

<script src="cannon.js"></script>

Parent page (index.html):

<script src="cannon.js"></script>
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • I'm not sure I understand how this would get a button click in the parent window to perform an action in the child window...? – thebjorn Apr 08 '19 at 08:03
0

I found the solution! Very simple: just use var actions; instead of let actions; to access the function. let limits the scope so it's not accessible.

More info here: What's the difference between using "let" and "var"?

devamat
  • 2,293
  • 6
  • 27
  • 50
  • Thanks for posting the solution for this issue. I suggest you to try to mark your own answer as an accepted answer for this question after 48 hrs, when it is available to mark. It can help other community members in future in similar kind of issues. Thanks for your understanding. – Deepak-MSFT Apr 09 '19 at 02:28