All Products
Search
Document Center

Mobile Platform as a Service:Toast

Last Updated:Feb 18, 2021

The interface is used to display a toast, and the toast duration can be configured.

Use toast interface

  1. AlipayJSBridge.call('toast', {
  2. content: 'Operation succeeds.',
  3. type: 'success',
  4. duration: 2000
  5. }, function() {
  6. alert ("Execute after toast disappears");
  7. });
  8. // You can hide the pop-up toast via the hideToast interface
  9. AlipayJSBridge.call('hideToast', {}, function() {
  10. });

Code sample

  1. <h1>Click the buttons below to see the different effects</h1>
  2. <a href="javascript:void(0)" class="btn success">Display success information</a>
  3. <a href="javascript:void(0)" class="btn fail">Display failure information</a>
  4. <a href="javascript:void(0)" class="btn exception">Display exception information</a>
  5. <a href="javascript:void(0)" class="btn none">Display information only</a>
  6. <a href="javascript:void(0)" class="btn duration">Display information for 3.5s</a>
  7. <script>
  8. function toast(config, callback){
  9. AlipayJSBridge.call('toast',config, callback);
  10. }
  11. function ready(callback) {
  12. // Call directly if JSBridge has been injected
  13. if (window.AlipayJSBridge) {
  14. callback && callback();
  15. } else {
  16. // Monitor the injected events if JSBridge hasn't been injected
  17. document.addEventListener('AlipayJSBridgeReady', callback, false);
  18. }
  19. }
  20. ready(function() {
  21. document.querySelector('.success').addEventListener('click', function() {
  22. toast({
  23. content: 'Operation succeeds',
  24. type: 'success'
  25. });
  26. });
  27. document.querySelector('.fail').addEventListener('click', function() {
  28. toast({
  29. content: 'Network is busy now, please try again later.',
  30. type: 'fail'
  31. });
  32. });
  33. document.querySelector('.exception').addEventListener('click', function() {
  34. toast({
  35. content: 'Attention, an exception has occurred.',
  36. type: 'exception'
  37. });
  38. });
  39. document.querySelector('.none').addEventListener('click', function() {
  40. toast({
  41. content: 'Welcome',
  42. });
  43. });
  44. document.querySelector('.duration').addEventListener('click', function() {
  45. toast({
  46. content: 'Please wait a moment',
  47. duration: 3500,
  48. }, function(e){
  49. alert ('Call back after toast disappears');
  50. });
  51. });
  52. });
  53. </script>

API

  1. AlipayJSBridge.call('toast', {
  2. content, type, duration
  3. }, fn)

Input parameters

Name Type Description Required Default value Version
content string Text content Y ‘’ 8.0
type string none / success / fail / exception. Text must be passed in case of toast for exceptions N ‘none’ 8.1
duration int Duration of display, in milliseconds N 2000 8.1
xOffset float Left is the positive direction, in pixels N 0 10.0.15
yOffset float Up is the positive direction, in pixels N 0 10.0.15
fn function Callback function, which is called when toast disappears N

Attentions

  • Toast can automatically close, but it can also be closed via hideLoading. Although this method is not common, it is necessary to prevent toast from being closed by hideLoading.a
  • For Android, if the system notification is turned off, the toast will not appear.
  • For the Android versions below 10.1.2, duration only supports 2000 and 3500. The duration less than or equal to 2000 is equivalent to 2000, and the one more than 2000 is equivalent to 3500.