2018-07-12 admin


什么是postscribe,Asynchronously write javascript, even with document.write.



### [](#npm)NPM

You can include `postscribe` using _npm_:

npm install --save postscribe

Postscribe runs in browsers, so this assumes you’re using a module bundler like webpack, Browserify, JSPM or Rollup to consume CommonJS modules.


You can include postscribe using bower by installing from the CDN URL:

bower install --save

The library will exist at <components_folder>/postscribe/index.js.



import postscribe from 'postscribe';


define(['postscribe'], function(postscribe) {



var postscribe = require('postscribe');


To append html to #mydiv:

postscribe('#mydiv', '<h1>Hello PostScribe</h1>');

In general:

postscribe(element, html, options);
  • element: a DOM Element, jQuery object, or id selector (e.g. “#mydiv”)
  • html: an html string or a function that takes a DOM Document and writes to it.
  • options: a hash of options
    • afterAsync: a callback called when an async script has loaded
    • afterDequeue: a callback called immediately before removing from the write queue
    • afterStreamStart: a callback called sync after a stream’s first thread release
    • afterWrite: a callback called after writing buffered document.write calls
    • done: a callback that will be called when writing is finished
    • autoFix: a boolean that allows disabling the autoFix feature of prescribe
    • beforeEnqueue: a callback called immediately before adding to the write queue
    • beforeWriteToken: a callback called before writing a token
    • beforeWrite: a callback called before writing buffered document.write calls
    • error: a function that throws the error by default, but could be overwritten
    • releaseAsync: a boolean whether to let scripts w/ async attribute set fall out of the queue

If you just want to mess around, include the js files at the top of an html page that contains the following:

<div id="mydiv"></div>
<script type="text/javascript">
  postscribe('#mydiv', '<h1>Hello PostScribe</h1>');

How to use PostScribe to render an ad after load

Where normally you would have

<div id="ad"><h5>Advertisement</h5>
  <script type="text/javascript">
    // Build url params and make the ad call
    document.write('<script src=doubleclick_url_with_params><\/script>');

Instead, remove the ad call and close the div

<div id="ad"><h5>Advertisement</h5></div>

<script type="text/javascript">
  // jQuery used as an example of delaying until load.
  $(function() {
    // Build url params and make the ad call
    postscribe('#ad', '<script src=doubleclick_url_with_params><\/script>');

There are some hooks you may pass as the third argument. For example:

<script type="text/javascript">
  // jQuery used as an example of delaying until load.
  $(function() {
    postscribe('#ad', '<script src=doubleclick_url_with_params><\/script>', {
      done: function() {'Dblclick script has been delivered.');


转载请注明:文章转载自 JavaScript中文网 []