1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
---
title: Navigator.share
slug: Web/API/Navigator/share
tags:
- Navitator
- Share
- Web Share
translation_of: Web/API/Navigator/share
---
<div>{{APIRef("HTML DOM")}}{{SeeCompatTable}} {{securecontext_header}}</div>
<p><strong><code>Navigator.share()</code></strong><font><font>方法通过调用本机的共享机制作为Web Share API的一部分。</font><font>如果不支持Web Share API,则此方法为</font></font><code>undefined</code><font><font>。</font></font></p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox notranslate">const sharePromise = window.navigator.share(<var>data</var>);
</pre>
<h3 id="参数">参数</h3>
<dl>
<dt><var>data</var></dt>
<dd><font>包含要共享的数据的对象。</font><font>必须至少指定以下字段之一。</font><font>可用选项包括:</font></dd>
</dl>
<ul>
<li><code>url</code>: 要共享的URL( {{domxref("USVString")}} )</li>
<li><code>text</code>: 要共享的文本( {{domxref("USVString")}} )</li>
<li><code>title</code>: 要共享的标题( {{domxref("USVString")}})</li>
<li><code>files</code>: 要共享的文件(“FrozenArray”)</li>
</ul>
<dl>
</dl>
<h3 id="返回值">返回值</h3>
<p>该方法将会返回一个{{jsxref("Promise")}}。一旦用户完成分享,这个promise将会接受 。如果指定的共享数据格式不正确,promise将会立即拒绝;如果用户取消了分享,promise也会拒绝。</p>
<p>例如, 在Android的Chrome上, 将在用户选择要共享的应用程序后将会解析共享的内容。.</p>
<h2 id="示例">示例</h2>
<pre class="brush: js notranslate">navigator.share({
title: document.title,
text: 'Hello World',
url: 'https://developer.mozilla.org',
}); // 分享MDN的URL</pre>
<h4 id="分享文件"><strong>分享文件</strong></h4>
<p>分享文件之前,先使用<code>navigator.canShare()</code>.判断这个文件能否被分享,Then include an array of files in the call to <code>navigator.share():</code></p>
<p>Notice: That the sample handles feature detection by testing for <code>navigator.canShare()</code> rather than for <code>navigator.share()</code>. The data object passed to <code>canShare()</code> only supports the <code>files</code> property. Image, video, audio, and text files can be shared.</p>
<pre class="brush: js notranslate">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.`);
}</pre>
<h2 id="规范">规范</h2>
<table>
<thead>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('Web Share API','#share-method','share()')}}</td>
<td>{{Spec2('Web Share API')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{Compat("api.Navigator.share")}}</p>
<h2 id="参见">参见</h2>
<ul>
<li>{{domxref("navigator.canShare()")}}</li>
</ul>
|