Supporting the onMessage Event in MooTools

By  on  

Yesterday I threw some window.postMessage knowledge right in your face.  The cross frame/window/domain technology that is window.postMessage is really interesting and as IE6 and IE7 fade away, window.postMessage will gain more momentum.  In looking to listen to onMessage events with MooTools, I noticed that message events aren't  handled properly.  The event type is seen as message but the event.data, event.source, and event.origin aren't added to the main-level object -- they're relegated to event.event.  It's time to fix that using MooTools custom events.

The MooTools JavaScript

Element.NativeEvents.message = 2;
Element.Events.message = {
	base: 'message',
	condition: function(event) {
		//if(event.type == 'message') {
		if(!event.$message_extended) {
			event.data = event.event.data;
			event.source = event.event.source;
			event.origin = event.event.origin;
			event.$message_extended = true;
		}
		return true;
	}
};

Regardless of whether or not the event type is within the Element.NativeEvents.message object, its value always matches what's provided by the browser, minus the "on" prefix.  With that in mind, creating a "custom" message event with "message" as the base is the way to go.  The condition portion of the custom event is met by the type being "message," so the only check is that the event hasn't been handled already.  If the condition is met, I move references to the data, origin, and source to the event object's first level to mimic the tradition message event. As an added bonus, if existing properties are undefined, I set their value to false.

The power of custom MooTools events is awesome.  window.onMessage is rarely used due to IE6 and IE7's crap and the lack of use case so onMessage may not be worth adding the code to Core.  If you do, however, need this functionality...here you go!

Recent Features

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

  • By
    7 Essential JavaScript Functions

    I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener and attachEvent.  Times have changed but there are still a few functions each developer should...

Incredible Demos

  • By
    NSFW Blocker Using MooTools and CSS

    One of my guilty pleasures is scoping out the latest celebrity gossip from PerezHilton.com, DListed.com, and JoBlo.com. Unfortunately, these sites occasionally post NSFW pictures which makes checking these sites on lunch a huge gamble -- a trip to HR's office could be just a click away. Since...

  • By
    MooTools HTML Police: dwMarkupMarine

    We've all inherited rubbish websites from webmasters that couldn't master valid HTML. You know the horrid markup: paragraph tags with align attributes and body tags with background attributes. It's almost a sin what they do. That's where dwMarkupMarine comes in.

Discussion

  1. I was entirely confused why window.addEvent( 'message', function(event) { ... }) was not working.
    This plugin is exactly what I needed, and now I know about Element.Events. Thanks!

    I am confused by the code comment //if(event.type == 'message') { and your post The condition portion of the custom event is met by the type being “message,” so the only check is that the event hasn’t been handled already..

    Is it necessary to check the type of the event when using MooTools custom events?
    Or, on the other hand, can I just remove that comment?

  2. What is the purpose of setting undefined values to false?

    for(key in event) {
        if(event[key] == undefined) {
            event[key] = false;
    }
    

    This modifies, for example, the alt, client, page, relatedTarget properties of the event object.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!