You are seeing the error because you are invoking the function immediately. You need to pass a reference to the function instead.
In other words, change this:
window.addEventListener('message', myFunction(event));
to this:
window.addEventListener('message', myFunction);
When using the method, the addEventListener() object will be passed as the first parameter by default when the event is fired.event
Try to make a test with code below. It is working with IE 11.
Main page code:
<html>
<head>
<title>Page Title</title>
<script>
//create popup window
var domain = 'http://example.com';
var myPopup = window.open(domain + '/HTML/popup_page.html','myWindow');
//periodical message sender
setInterval(function(){
var message = 'Hello! The time is: ' + (new Date().getTime());
console.log('blog.local: sending message: ' + message);
myPopup.postMessage(message,domain); //send the message and target URI
},6000);
//listen to holla back
window.addEventListener('message',function(event) {
if(event.origin !== 'http://example.com') return;
console.log('received response: ',event.data);
},false);
</script>
</head>
<body>
<h1>Main page</h1>
</body>
</html>
Popup page code:
<!Doctype html>
<html>
<head>
<script>
window.addEventListener('message',function(event) {
if (event.origin !== 'http://example.com') return;
alert("OK popup");
console.log('message received: ' + event.data,event);
event.source.postMessage('holla back youngin!',event.origin);
},false);
</script>
</head>
<body>
<h1>popup_page</h1>
</body>
</html>
Output in IE:


Note that you will also get an alert() while you run the code.
Reference:
HTML5’s window.postMessage API
You need to use some concrete event name like for example - and click on browser page window - eg:click
window.addEventListener('click', function() {
console.log("some_event triggered");
});
Here is some info about events and situations when they are trigger.
Yes you can define and fire arbitrary events - read here. And here is some example code:
var event = new Event('some_event');
// Listen for the event.
window.addEventListener('some_event', function (e) {
console.log('some_event triggered')
}, false);
// Dispatch the event.
window.dispatchEvent(event);
So you fire event using and this is the way you can test it. window.dispatchEvent(event)
Ok So I have created a fiddle that will show you how to remove and add the message handler and how to post message it's here:
new Vue({
el: "#app",
methods: {
addHandler: function() {
window.addEventListener('message', this.eventListenerExample);
},
removeHandler: function(){
window.removeEventListener('message', this.eventListenerExample);
},
postMessage: function() {
window.postMessage("This is a message ");
},
eventListenerExample: function(event){
console.log(event, ' i am here');
}
},
mounted: function () {
this.addHandler();
},
destroyed: function() {
this.remveHanlder();
}
})
https://jsfiddle.net/ob1pLd7z/8/
Note here that the function is part of this vue instance and the function itself is added and removed rather than using an inline function like you originally showed.
While you using to declare functions, it should be declare before you call the function. var x = function(){}
By , function x (){} will exist in current scope, no matter how later it declare. Because such side effect, the x are more recommended. For example:var x = function(){}
var user = 'alien';
if( user == 'alien') {
function salute () { console.log("Welcome to Earth!"); }
} else {
function salute (){ console.log("Good day!"); }
}
The will print salute() anyway, even though it not we want at all.Good day!