Navigator.share
Experimental: 这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。
Navigator.share()
方法通过调用本机的共享机制作为 Web Share API 的一部分。如果不支持 Web Share API,则此方法为 undefined
。
语法
const sharePromise = window.navigator.share(data);
参数
返回值
该方法将会返回一个Promise
。一旦用户完成分享,这个 promise 将会接受。如果指定的共享数据格式不正确,promise 将会立即拒绝;如果用户取消了分享,promise 也会拒绝。
例如,在 Android 的 Chrome 上,将在用户选择要共享的应用程序后将会解析共享的内容。
示例
navigator.share({
title: document.title,
text: 'Hello World',
url: 'https://developer.mozilla.org',
}); // 分享 MDN 的 URL
分享文件
分享文件之前,先使用 navigator.canShare()
判断这个文件能否被分享,Then include an array of files in the call to navigator.share()
:
Notice: That the sample handles feature detection by testing for navigator.canShare()
rather than for navigator.share()
. The data object passed to canShare()
only supports the files
property. Image, video, audio, and text files can be shared.
if (navigator.canShare && navigator.canShare({ files: filesArray })) {
navigator.share({
files: filesArray,
title: 'Pictures',
text: 'Our Pictures.',
})
.then(() => console.log('Share was successful.'))
.catch((error) => console.log('Sharing failed', error));
} else {
console.log(`Your system doesn't support sharing files.`);
}
规范
Specification |
---|
Web Share API # share-method |
浏览器兼容性
BCD tables only load in the browser