The Navigator.canShare()
method of the Web Share API returns true
if the equivalent call to navigator.share()
would succeed.
The method returns false
if the data cannot be validated. Reasons the data might be invalid include:
- The
data
parameter has been omitted or only contains properties with unknown values. Note that any properties that are not recognized by the user agent are ignored. - A URL is badly formatted.
- Files are specified but the implementation does not support file sharing.
- Sharing the specified data would be considered a "hostile share" by the user-agent.
The Web Share API is gated by the web-share permission policy. The canShare()
method will return false
if the permission is supported but has not been granted.
canShare()
canShare(data)
Returns true
if the specified data
can be shared with Navigator.share()
, otherwise false
.
The example uses navigator.canShare()
to check whether navigator.share()
can share the specified data.
HTML
The HTML just creates a paragraph in which to display the result of the test.
JavaScript
let shareData = {
title: "MDN",
text: "Learn web development on MDN!",
url: "https://developer.mozilla.org",
};
const resultPara = document.querySelector(".result");
if (!navigator.canShare) {
resultPara.textContent = "navigator.canShare() not supported.";
} else if (navigator.canShare(shareData)) {
resultPara.textContent =
"navigator.canShare() supported. We can use navigator.share() to send the data.";
} else {
resultPara.textContent = "Specified data cannot be shared.";
}
Result
The box below should state whether navigator.canShare()
is supported on this browser, and if so, whether or not we can use navigator.share()
to share the specified data:
This method feature tests whether a particular data property is valid and shareable. If used with a single data
property it will return true
only if that property is valid and can be shared on the platform.
The code below demonstrates verifying that a data property is supported.
let testShare = { someNewProperty: "Data to share" };
const shareData = {
title: "MDN",
text: "Learn web development on MDN!",
url: "https://developer.mozilla.org",
someNewProperty: "Data to share",
};
if (navigator.canShare(testShare)) {
} else {
}