-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsharing.html
More file actions
52 lines (42 loc) · 1.3 KB
/
sharing.html
File metadata and controls
52 lines (42 loc) · 1.3 KB
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
<!DOCTYPE html>
<html>
<head>
<title>
How to use web share API for native
share options in HTML and JavaScript?
</title>
</head>
<body style="text-align: center;">
<h2>Web Share API Demonstration</h2>
<button id="btn">Share</button>
<script>
const btn = document.getElementById("btn");
// function for web share api
function webShareAPI(header, description, link) {
navigator
.share({
title: header,
text: description,
url: link
})
.then(() => console.log("Successful share"))
.catch((error) => console.log("Error sharing", error));
}
// Fallback, Tries to use API only
// if navigator.share function is
// available
if (navigator.share) {
// Show button if it supports webShareAPI
btn.style.display = "block";
btn.addEventListener("click", () =>
webShareAPI("header", "description", "www.url.com")
);
} else {
// Hide button if it supports webShareAPI
btn.style.display = "none";
console.error("Your Browser doesn't support Web Share API");
document.write("Your Browser doesn't support Web Share API");
}
</script>
</body>
</html>