JSAPI

Since 8.0

toast

Prompts a toast notification, its display duration is configurable. Note for Android platform, this API is not effective if user has disabled the system notification.

Usage

AlipayJSBridge.call('toast', {
  content: 'Success',
  type: 'success',
  duration: 2000
}, function() {
  alert("Execute when toast dismisses");
});

// You can also manually hide the toast via hideToast API
AlipayJSBridge.call('hideToast', {}, function() {
});


Example

Various use cases

<h1>Please click the following buttons</h1>
<a href="javascript:void(0)" class="btn success">Display success</a>
<a href="javascript:void(0)" class="btn fail">Display failure</a>
<a href="javascript:void(0)" class="btn exception">Display exception</a>
<a href="javascript:void(0)" class="btn none">Display message</a>
<a href="javascript:void(0)" class="btn duration">Display toast for 3.5 seconds</a>

<script>
function toast(config, callback){
  AlipayJSBridge.call('toast',config, callback);
}

function ready(callback) {
  // Invoke directly if JSBridge is already injected
  if (window.AlipayJSBridge) {
    callback && callback();
  } else {
    // Otherwise listen to `AlipayJSBridgeReady` event
    document.addEventListener('AlipayJSBridgeReady', callback, false);
  }
}
ready(function() {
  document.querySelector('.success').addEventListener('click', function() {
    toast({
      content: 'Success',
      type: 'success'
    });
  });

  document.querySelector('.fail').addEventListener('click', function() {
    toast({
      content: 'Network busy, please try again later',
      type: 'fail'
    });
  });

  document.querySelector('.exception').addEventListener('click', function() {
    toast({
      content: 'Exception occurs',
      type: 'exception'
    });
  });

  document.querySelector('.none').addEventListener('click', function() {
    toast({
      content: 'Welcome',
    });
  });

  document.querySelector('.duration').addEventListener('click', function() {
    toast({
      content: 'Please wait',
      duration: 3500,
    }, function(e) {
      alert('Callback when toast dismisses');
    });
  });
});
</script>

API

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

Input Parameters

NameTypeDescriptionMandatoryDefault
contentstringText content of this toastY‘’
typestringAvailable options: none / success / fail / exceptionN‘none’
durationintDisplay duration, in millisecondsN2000
xOffsetfloatright to left by pxN010.0.15
yOffsetfloatbottom to top by pxN010.0.15
fnfunctionCallback function, invoked when toast dismissesN

Remarks

  • Although toast dismisses after a certain duration, you can manually dismiss it by calling hideLoading API.
  • For Android platform, this API is not effective if user has disabled the system notification.