javascript iframe event listener code example

Example 1: how to add event listener to iframe

var iframe = document.getElementById('myIFrame');
iframe.contentWindow.body.addEventListener('mouseup', Handler);

function Handler() {
    alert('works');
}

Example 2: apply eventlistener to iframe

var iframe = document.getElementsByTagName('iframe')[0],
    iDoc = iframe.contentWindow     // sometimes glamorous naming of variable
        || iframe.contentDocument;  // makes your code working :)
if (iDoc.document) {
    iDoc = iDoc.document;
    iDoc.body.addEventListener('afterLayout', function(){
                        console.log('works');
                });
};