Aaronius / penpal

A promise-based library for securely communicating with iframes via postMessage.
MIT License
389 stars 56 forks source link

Fix problems with document parsing sinchronicity #41

Closed corollari closed 4 years ago

corollari commented 4 years ago

Currently the code present in the readme will cause an exception if executed before the document has been parsed, this PR fixes that by preventing that from happening.

How to reproduce the error

Place the readme's code in a script tag inside the <head> element of an html page. More concretely, what I'm doing is building the readme's code along with the penpal library into a single js file using webpack and then importing that script into a page in the following way:

<html>
  <head>
    <script src="readme-code-bundle.js"></script>
  </head>
  <body></body>
</html>

How this PR fixes the problem

It makes the code check if the document has already been parsed and, in case that it hasn't, it sets up an event that will be triggered when the document is ready.

Aaronius commented 4 years ago

Thanks @corollari!