Skip to content

Commit 2fa091f

Browse files
adding web share demo
1 parent 224c7f1 commit 2fa091f

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,5 @@ Code examples that accompany various MDN DOM and Web API documentation pages.
4242
* The "touchevents" directory is for examples and demos of the [Touch Events](https://developer.mozilla.org/en-US/docs/Web/API/Touch_events) standard.
4343

4444
* The "web-storage" directory contains a simple demo to show usage of the Web Storage API. For more detail on how it works, read [Using the Web Storage API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API). [View the demo live](http://mdn.github.io/dom-examples/web-storage/).
45+
46+
* * The "web-share" directory contains a simple demo to show usage of the [Web Share API](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/share). For more detail on how it works, read. [View the demo live](http://mdn.github.io/dom-examples/web-share/).

web-share/index.html

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Web share test</title>
6+
<style>
7+
8+
</style>
9+
</head>
10+
<body>
11+
<h1>Sharing MDN</h1>
12+
13+
<p>We love MDN, and want to share it as far as we can! Click the following button to share MDN's home page using your system's native share functionality. See the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Navigator/share#Browser_compatibility">browsers this currently works on</a>.</p>
14+
15+
<p><button>Share MDN!</button></p>
16+
17+
<p class="result"></p>
18+
19+
<script>
20+
let shareData = {
21+
title: 'MDN',
22+
text: 'Learn web development on MDN!',
23+
url: 'https://developer.mozilla.org',
24+
}
25+
26+
const btn = document.querySelector('button');
27+
const resultPara = document.querySelector('.result');
28+
29+
btn.addEventListener('click', () => {
30+
navigator.share(shareData)
31+
.then(() =>
32+
resultPara.textContent = 'MDN shared successfully'
33+
)
34+
.catch((e) =>
35+
resultPara.textContent = 'Error: ' + e
36+
)
37+
});
38+
</script>
39+
</body>
40+
</html>

0 commit comments

Comments
 (0)