window.addEventListener message function not working in wordpress

To get the height of the iframe from other domain url i am using below javascript code to set the height of the iframe depending upon content from iframe url.

window.addEventListener('message', function(event) {
  if (event.origin !== 'https://example.com') return; // only accept messages from the specified domain
  if (isNaN(event.data)) return; // only accept something which can be parsed as a number
  var height = parseInt(event.data) + 32; // add some extra height to avoid scrollbar
  //alert(height);
  document.getElementById('form-iframe').height = height + "px";
}, false);

The height of iframe would be posted from iframe url as

Read More
parent.postMessage(document.body.scrollHeight, 'https://example.com');

But if i use above script in wordpress it is not working and it is not going into addEventListener function. Can anyone help me to sort out this problem.

Related posts