All Products
Search
Document Center

Initialization

Last Updated: Mar 24, 2021

After window.onload, the container starts initialization to generate the global variable AlipayJSBridge, and then triggers the event of JSBridge initialization complete (AlipayJSBridgeReady).

Notice:

  • AlipayJSBridge injection is an asynchronous process, and it’s necessary to call AlipayJSBridgeReady after monitoring the event.
  • Be sure to use the ready method to perform initialization, otherwise the HTML5 container may fail to obtain AlipayJSBridge.

Use AlipayJSBridgeReady interface

  1. function ready(callback) {
  2. // Call directly if JSBridge has been injected
  3. if (window.AlipayJSBridge) {
  4. callback && callback();
  5. } else {
  6. // Monitor the injected events if JSBridge hasn't been injected
  7. document.addEventListener('AlipayJSBridgeReady', callback, false);
  8. }
  9. }

Code sample

The following code sample is the standard writing format for bridge entry:

  1. <h1>Method to use the Bridge </h1>
  2. <script>
  3. function ready(callback) {
  4. if (window.AlipayJSBridge) {
  5. callback && callback();
  6. } else {
  7. document.addEventListener('AlipayJSBridgeReady', callback, false);
  8. }
  9. }
  10. ready(function() {
  11. alert('bridge ready');
  12. });
  13. </script>