This interface is used to roll back multiple levels of pages at a time.
Note: It is only allowed to popTo the pages within the current App instance, and the cross-appId jump is not allowed.
Use popTo interface
// Close the currently opened pageAlipayJSBridge.call('popTo', {index: -1});
Code example
Close the current page and pass data:
<h1>Click "Execute" to close the current page and return data</h1><a href="#" class="btn J_demo">Execute</a><script>function ready(callback) {// Call directly if JSBridge has been injectedif (window.AlipayJSBridge) {callback && callback();} else {// Monitor the injected events if JSBridge hasn't been injecteddocument.addEventListener('AlipayJSBridgeReady', callback, false);}}ready(function(){document.querySelector('.J_demo').addEventListener('click', function() {// The passed data object will be received by the resume event on the upcoming page.AlipayJSBridge.call('popTo', {index: -1, // Return to the previous page, and if there is no previous page at this time, an error will be reported.data: { // Important: data is a dictionary rather than an arrayfrom: location.href,info: Date.now()}}, function(e) { // Add a callback, because popTo will not necessarily succeed (an error will be reported when the current page is the only one opened)alert(JSON.stringify(e));});});});</script>
Return to the page that complies with regulation match through
urlPattern:<h1>Return to the URL that complies with a certain rule</h1><h3></h3><a href="javascript:void(0)" class="btn J_new_window">Open the current page in a new window</a><a href="javascript:void(0)" class="btn J_demo">Return</a><script>var query = getQuery();var depth = (+query.depth) || 0;document.querySelector('h3').innerHTML = 'current page depth: ' + depth;function ready(callback) {// Call directly if JSBridge has been injectedif (window.AlipayJSBridge) {callback && callback();} else {// Monitor the injected events if JSBridge hasn't been injecteddocument.addEventListener('AlipayJSBridgeReady', callback, false);}}ready(function(){document.querySelector('.J_demo').addEventListener('click', function() {AlipayJSBridge.call('popTo', {urlPattern: 'pop-to-url-pattern.html',}, function(e) {alert(JSON.stringify(e));});});document.querySelector('.J_new_window').addEventListener('click', function() {AlipayJSBridge.call('pushWindow', {url: location.pathname + '?depth=' + (1+depth),});});});</script>
API
AlipayJSBridge.call('popTo',{index, urlPattern}, fn)
index and urlPattern refer to two query modes, which should not be used at the same time.
Input parameters
| Name | Type | Description | Required | Default value | Version |
|---|---|---|---|---|---|
| index | int | The index of the target interface in the session interface stack. If it is less than 0, it will be added to the index of the current interface. | Y | ||
| urlPattern | string | The URL matching expression for the target interface (if URL contains urlPattern, the matching is successful) |
Y | ||
| fn | function | When the operation is successful, the callback may not be called; when the operation fails, the callback function is executed | N |
Out parameter
| Name | Type | Description |
|---|---|---|
| result | undefined | The callback may not be called when the operation is successful; result should not be used |
Error code
| Error code | Description |
|---|---|
| 10 | No arguments are configured; Invalid index; Failed to match urlPattern |
Attentions
- In general,
popTois used for scenarios that can be completed in multiple steps, such as returning when user information is complete, and returning after the 3-level address is selected. - When returning via
urlPattern,popTowill return to the page furthest from the current page, which is the bottom of the stack. Meanwhile, it will not check whether the URL of the current page matches. - Please check the pResume event for information on how the data carried in
popTois received.