1

I have a application which loads two html pages , I want to fire custom event from one html page which will be listened by other html page , is it possible ? Right now I have fired custom event but its listened only by the same page , other page is not listening to this event.

Here is my code in first html page::

var event= new CustomEvent(
    "myeventtype", 
    {
        detail: {
            name: "",
            details: ""
        },
        bubbles: true,
        cancelable: true
    }
);
window.dispatchEvent(event );

//handler
function myHandler(e) {
alert(e.detail.name);

}

//listener

window.addEventListener("myeventtype", myHandler);

second html page only add listener and handler::

window.addEventListener("myeventtype", testHandler);

//handler
    function testHandler(e) {
    alert(e.detail.name);

    }

why my second page is not listening to this event, am i doing something wrong here, please get me out of this.

thanks,

Akhil Aravind
  • 5,741
  • 16
  • 35
pbhle
  • 2,856
  • 13
  • 33
  • 40
  • Possible duplicate of [Communication between tabs or windows](https://stackoverflow.com/questions/28230845/communication-between-tabs-or-windows) – pretzelhammer Jul 16 '18 at 05:33

1 Answers1

0

No, it is not possible to create an object or function and use it on others pages. But you can add create a js file and then link those js file in both HTML pages. I hope it will be workable.

Amin Mithun
  • 25
  • 1
  • 5