Message Event Handler Exist Or Not In Window Object
I have a page in HTML that contains iframe. If message comes from iframe I have to handle it. But how to check if message event handler exist or not? My code is $(window).on('messa
Solution 1:
Try
$(function () {
$("body").text("click");
// `message` `handler`window.msgHandler = function (e) {
if (e.origin === "http://fiddle.jshell.net") {
console.log(e, e.data);
};
};
window.addEventListener('message', msgHandler, false);
var iframe = $("<iframe>", {
"width": "150px",
"height": "150px",
"target": "_top"
});
$(iframe).one("load", function (e) {
var _data = $(e.target)[0].nodeName + " loaded at " + e.timeStamp;
window.postMessage(_data, "http://fiddle.jshell.net");
$(e.target)
.contents()
.find("html")
.html("<html><body><div>"
+ _data
+ "</div><br /></body></html>");
returnfalse
});
var loadFrame = function (e) {
// if in window object is message event handler present or notconsole.log("msgHandler"inwindow
? [ window.msgHandler
, window.msgHandler.toString()
, typeofwindow.msgHandler ]
: false);
$(e.target)
.text("")
.append($(iframe))
};
$(window).one("click", loadFrame);
});
Post a Comment for "Message Event Handler Exist Or Not In Window Object"