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 callAlipayJSBridgeReady
after monitoring the event.- Be sure to use the
ready
method to perform initialization, otherwise the HTML5 container may fail to obtainAlipayJSBridge
.
Use AlipayJSBridgeReady interface
function ready(callback) {
// Call directly if JSBridge has been injected
if (window.AlipayJSBridge) {
callback && callback();
} else {
// Monitor the injected events if JSBridge hasn't been injected
document.addEventListener('AlipayJSBridgeReady', callback, false);
}
}
Code sample
The following code sample is the standard writing format for bridge
entry:
<h1>Method to use the Bridge </h1>
<script>
function ready(callback) {
if (window.AlipayJSBridge) {
callback && callback();
} else {
document.addEventListener('AlipayJSBridgeReady', callback, false);
}
}
ready(function() {
alert('bridge ready');
});
</script>