0

I want call a javascript function defined in parent on child window. I have searched for window.opener and window.parent, but they call function on parent window not on child. Also if it is possible by local storage. Parent window function:

var myFunction=function download() {
        console.log("in jsp download")
        $("#btnExport").click(function(e) {
            e.preventDefault();

            //some operation
        });
    }

    window.onload = function() {
        localStorage.setItem("storageName",myfunction);

    }

Child window

<button id="exportExcel" onclick="localStorage.getItem("storageName")">Export</button>

But this is giving error on child window

shiva kumar
  • 21
  • 1
  • 10
  • What do you mean by "on child window"? It sounds like you actually just want to pass a parameter. – SLaks Jun 07 '17 at 18:31
  • I don't think it's good idea to store a function in localstorage. https://stackoverflow.com/questions/11063630/save-a-function-in-localstorage – Kashkain Jun 07 '17 at 18:31
  • I meant that I want to run parent javascript function on child page not parent page. – shiva kumar Jun 07 '17 at 18:33

1 Answers1

2

When you create the function you're attaching it to the parent's scope. If the parent page opens the child (via window.open) you can access its scope through the child's window.opener object.

Parent Page

Function DoSomething(){}

Child Page

window.opener.DoSomething()
Sugarcaen
  • 90
  • 7