This API sets the properties of the button in the upper-right corner of the title bar. To display the button, you must also call showOptionMenu.
Due to Apple's App Transport Security (ATS) restrictions, the icon URL must be an HTTPS link or a Base64 string. HTTP links are ignored.
Use the setOptionMenu API
AlipayJSBridge.call('setOptionMenu', {
title : 'Button', // Choose one from title, icon, and icontype
redDot : '-1', // -1: Do not display. 0: Display a red dot. 1-99: Display a number on the red dot.
color : '#ff00ff00', // Must be an ARGB color value that starts with #.
});Code example
Set different types of buttons in the upper-right corner:
<h1>Click the buttons below to see different effects</h1>
<a href="javascript:void(0)" class="btn button">Single button</a>
<a href="javascript:void(0)" class="btn icon">Single icon</a>
<a href="javascript:void(0)" class="btn menu">Multiple menus (9.9.3)</a>
<a href="javascript:void(0)" class="btn reset">Reset</a>
<a href="javascript:void(0)" class="btn hide">Hide</a>
<a href="javascript:void(0)" class="btn show">Show</a>
<script>
function ready(callback) {
// If jsbridge is already injected, call it directly.
if (window.AlipayJSBridge) {
callback && callback();
} else {
// If not, listen for the injection event.
document.addEventListener('AlipayJSBridgeReady', callback, false);
}
}
ready(function(e) {
document.querySelector('.button').addEventListener('click', function() {
AlipayJSBridge.call('setOptionMenu', {
title : 'Button',
redDot : '5', // -1: Do not display. 0: Display a red dot. 1-99: Display a number on the red dot.
color : '#ff00ff00', // Must be an ARGB color value that starts with #.
});
AlipayJSBridge.call('showOptionMenu');
});
document.querySelector('.icon').addEventListener('click', function() {
AlipayJSBridge.call('setOptionMenu', {
icon : 'http://pic.alipayobjects.com/e/201212/1ntOVeWwtg.png',
redDot : '-1', // -1: Do not display. 0: Display a red dot. 1-99: Display a number on the red dot.
});
AlipayJSBridge.call('showOptionMenu');
});
document.querySelector('.menu').addEventListener('click', function() {
AlipayJSBridge.call('setOptionMenu', {
// The display order is from right to left.
menus: [{
icontype: 'scan',
redDot: '-1', // -1: Do not display. 0: Display a red dot. 1-99: Display a number on the red dot.
}, {
icontype: 'user',
redDot: '-1', // -1: Do not display. 0: Display a red dot. 1-99: Display a number on the red dot.
}],
override: true // If you set multiple options, specifies whether to keep the default optionMenu.
});
// Call this once to refresh the interface.
AlipayJSBridge.call('showOptionMenu');
});
document.querySelector('.reset').addEventListener('click', function() {
AlipayJSBridge.call('setOptionMenu', {
reset: true,
});
AlipayJSBridge.call('showOptionMenu');
});
document.querySelector('.show').addEventListener('click', function() {
AlipayJSBridge.call('showOptionMenu');
});
document.querySelector('.hide').addEventListener('click', function() {
AlipayJSBridge.call('hideOptionMenu');
});
document.addEventListener('optionMenu', function(e) {
alert(JSON.stringify(e.data));
}, false);
});
</script>API description
If the button does not appear as expected after you call setOptionMenu, call showOptionMenu.
Set only one of the following four properties. The properties are prioritized in this order: `reset` > `title` > `icontype` > `icon`.
AlipayJSBridge.call('setTitle',{
title, icon, redDot, reset, color, override, menus, icontype
})Input parameters
Property | Type | Description | Required | Default value |
title | string | The text of the button on the right. | Yes | "" |
icon | string | The URL of the icon for the button on the right. Supports Base64 since version 9.0. Versions 8.3 and earlier: iOS 40 × 40 px (no padding), Android 50 × 50 px (5 px transparent padding on each side). Versions 8.4 and later: Both platforms use 40 × 40 px (no padding). | Yes | "" |
redDot | string | The value for the red dot. | No | "" |
reset | bool | Resets to the system default. When reset is | Yes | false |
color | string | The text color value. | N | "#FFFFFFFF" |
override | bool | If you set multiple options, specifies whether to keep the default optionMenu. | N | false |
menus | array | Sets multiple buttons. | N | [] |
preventDefault | bool | Specifies whether to prevent the default sharing feature, which opens a sharing dialog box. If `preventDefault` is `true`, the default sharing feature is prevented. | N | [] |
icontype | string | Loads a preset container image based on the image type. Choose one from `icontype`, `title`, and `icon`. Important Color changes are supported only for a single optionMenu. Valid types include `user` (account), `filter`, `search`, `add`, `settings`, `scan`, `info`, `help`, `locate`, `more`, and `mail` (version 10.0.8 and later). | N | "" |
contentDesc | string | Sets the text to be read in screen reader mode. | N | "" |