postscribe

2018-07-12 admin

postscribe是什么

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

postscribe介绍、postscribe使用

</script>


### [](#npm)NPM

You can include `postscribe` using _npm_:

```text-shell-session
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.

Bower

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

bower install --save https://cdnjs.cloudflare.com/ajax/libs/postscribe/2.0.8/postscribe.min.js

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

Accessing

ES6/ES2015

import postscribe from 'postscribe';

AMD

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

});

CommonJS

var postscribe = require('postscribe');

Usage

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>');
</script>

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>');
  </script>
</div>

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>');
  });
</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() {
        console.info('Dblclick script has been delivered.');
      }
    });
  });

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-35823.html

文章标题:postscribe

回到顶部