<script type="text/javascript"> $.fn.extend({ addEvent: function (type, handle, bool) { var el, thisLen = this.length; bool ? bool = bool : bool = false; if (thisLen == 1) { el = this[0]; //jquery对象转成 js对象 el.addEventListener ? el.addEventListener(type, handle, bool) : el.attachEvent('on' + type, handle); } else { for (var i = 0; i < thisLen; i++) { el = this[i]; el.addEventListener ? el.addEventListener(type, handle, bool) : el.attachEvent('on' + type, handle); } } } }); </script>
兼容IE 与Firefox浏览器
var messageInfo = new Object; messageInfo.received = '/HumanResource/Employee/Index'; messageInfo.infoType = 'technical lead'; messageInfo.data = 'technical lead'; parent.postMessage(JSON.stringify(messageInfo), window.location);
子窗口向父窗口传递消息
$(window).addEvent('message', function (ev) { var messageInfoJson = JSON.parse(ev.data); var received = messageInfoJson.received; if (received != '/Home/Index') { console.log('转发消息'); $("iframe").each(function (i) { console.log(ev.data); console.log(messageInfoJson); window.frames[i].postMessage(ev.data, window.location); }); } else { alert('哦,Home index 我被点啦。。。' + messageInfo.data); console.log('哦,Home index 我被点啦。。。'); console.log(ev); } }, false);
接受消息,如不需要自己处理,则转发消息给其他窗口