concept window in category javascript

appears as: window, window, windows
Third-Party JavaScript

This is an excerpt from Manning's book Third-Party JavaScript.

The function expression from listing 2.2 has two parameters: window and undefined. You might recognize these as global objects that should be accessible at every scope. So why redeclare them as function arguments?

For starters, window and undefined are two objects that you’ll likely use repeatedly in your code. When they’re declared as local variables like this, a JavaScript minifier can shorten their variable names to help reduce the script’s file size. But if they’re referenced as global variables, those references can’t be renamed and shortened.

After you have a reference to the target Window object, you can send messages to that window by calling a special postMessage function on that object. This function accepts two formal parameters: data to send (the message body) and the target’s origin value:

Window {
  void postMessage(any message, string targetOrigin)
}

With this signature in mind,[2] you can now send your first message. Here’s a quick example where the message body is Hello, World!, the target origin is thirdpartyjs.com, and the Window object is referenced by a variable win:

5.1.2. Receiving messages sent to a window

As you might’ve noticed, when the postMessage function sends a message, it doesn’t request any confirmation from the recipient window. It publishes the message to the window, and the code inside the window must subscribe to a special event if it’s interested in the incoming correspondence.

Subscribing to new messages is straightforward: you attach a listener function to the window’s message event. Your listener will be called every time a new message is posted to the window from anywhere using the postMessage function. Because this is an ordinary browser event (like a mouse click event), your listener function will have access to an Event instance object. That object exposes three properties:

The code in listing 5.2 implements the diagram we showed you in figure 5.1. The receiver listens to all messages but recognizes only those that originate from camerastork.com. If the message from camerastork.com contains a string "hello", the receiver sends back a message with a string "world". Note that messages between window objects are all done on the client—they don’t perform any network requests and are blazingly fast.

Listing 5.2. Event handler rejects messages that don’t originate from camerastork.com.

As you can see from the examples, the window.postMessage API is simple and it solves just one problem—the problem of exchanging messages between windows. Everything else—from simple origin checks to the complex relationships between messages—is outside of the window.postMessage specification and must be handled by you. We’ll show you how to build a simple request/response system later in this chapter. But now let’s talk about something that most often kills all the excitement about shiny, new HTML or JavaScript features. Let’s talk about browser compatibility. (Spoiler: it’s not that bad.)