ArticleslgStudy

science

Web Messaging

Web Messaging is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Web Messaging rather than just read about it. In short: Web Messaging, or cross-document messaging, is an API introduced in the WHATWG HTML5 draft specification, allowing documents to communicate with one another across different origins, or source domains while rendered in a web browser. Prior to HTML5, web browsers disallowed cross-site scripting, to protect against security attacks.

Key takeaways

  • Web Messaging belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Web Messaging to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Web Messaging from memory before moving on to harder problems.

Reference excerpt

Web Messaging, or cross-document messaging, is an API introduced in the WHATWG HTML5 draft specification, allowing documents to communicate with one another across different origins, or source domains while rendered in a web browser. Prior to HTML5, web browsers disallowed cross-site scripting, to protect against security attacks. This practice barred communication between non-hostile pages as well, making document interaction of any kind difficult. Cross-document messaging allows scripts to interact across these boundaries, while providing a rudimentary level of security.

Requirements and attributes Using the Messaging API's postMessage method, plain text messages can be sent from one domain to another, e.g. from a parent document to an IFRAME. This requires that the author first obtain the Window object of the receiving document. As a result, messages can be posted to the following:

other frames or iframes within the sender document's window windows the sender document explicitly opens through JavaScript calls the parent window of the sender document the window which opened the sender document The message event being received has the following attributes:

data – The data, or actual content, of the incoming message. origin – The origin of the sender document. This typically includes the scheme, hostname and port. It does not include the path or fragment identifier. source – the WindowProxy of where the document came from (the source window). postMessage is not a blocking call; messages are processed asynchronously.

Example Consider we want document A loaded from example.net to communicate with document B loaded from example.com into an iframe or popup window. The JavaScript for document A will look as follows:

The origin of our contentWindow object is passed to postMessage. It must match the origin of the document we wish to communicate with (in this case, document B). Otherwise, a security error will be thrown and the script will stop. The JavaScript for document B will look as follows:

An event listener is set up to receive messages from document A. Using the origin property, it then checks that the domain of the sender is the expected domain. Document B then looks at the message, either displaying it to the user, or responding in turn with a message of its own for document A.

Security Poor origin checking can pose a risk for applications which employ cross-document messaging. To safeguard against malicious code from foreign domains, authors should check the origin attribute to ensure messages are accepted from domains they expect to receive messages from. The format of incoming data should also be checked that it matches the expected format.

Support Support for cross-document messaging exists in current versions of Internet Explorer, Mozilla Firefox, Safari, Google Chrome, Opera, Opera Mini, Opera Mobile, and Android web browser. Support for the API exists in the Trident, Gecko, WebKit and Presto layout engines.

See also Cross-site scripting Cross-site request forgery Same-origin policy Cross-origin resource sharing JSONP

References

External links HTML5 Web Messaging recommendation HTML5 Web Messaging specification Archived 2010-12-24 at the Wayback Machine Cross-Document Messaging – HTML Draft Standard, WHATWG WebKit DOM Programming Topics - Cross Document Messaging, Apple Developer Library, archived from the original on 2012-06-09, retrieved 2013-12-29 Eng, Chris (2010-05-17), HTML5 Security in a Nutshell, Veracode When can I use Cross-Document Messaging?, CanIUse A Selection of Supported Features in HTML5, archived from the original on 2011-11-12, retrieved 2011-04-18

Worked examples

Example 1 — a first encounter with Web Messaging

Start with the simplest possible case. Write down what Web Messaging claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Web Messaging before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Web Messaging ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Web Messaging

In research
Web Messaging appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Web Messaging in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Web Messaging is common in secondary-school and first-year university syllabi. It links to neighbouring topics Application programming interfaces, HTML, so understanding it makes those chapters shorter.
In everyday life
Look for Web Messaging outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Web Messaging” →

How to study Web Messaging in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Web Messaging means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Web Messaging out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Web Messaging in simple terms?

Web Messaging, or cross-document messaging, is an API introduced in the WHATWG HTML5 draft specification, allowing documents to communicate with one another across different origins, or source domains while rendered in a web browser. Prior to HTML5, web browsers disallowed cross-site scripting, to…

Why does Web Messaging matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Web Messaging?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Web Messaging.

Tags

  • Application programming interfaces
  • HTML

Keep exploring