After window.onload executes, the container initializes. This process creates the AlipayJSBridge global variable and triggers the AlipayJSBridgeReady event.
Important
Because
AlipayJSBridgeis injected asynchronously, always listen for theAlipayJSBridgeReadyevent before you make calls to the bridge.Always use the ready method for initialization. Otherwise, the HTML5 page might fail to obtain
AlipayJSBridge.
How to use AlipayJSBridgeReady
function ready(callback) {
// If the JS Bridge is ready, call the callback.
if (window.AlipayJSBridge) {
callback && callback();
} else {
// Otherwise, listen for the ready event.
document.addEventListener('AlipayJSBridgeReady', callback, false);
}
}Code example
The following code example shows the standard entry point for the bridge.
<h1>How 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>