files/en-us/web/api/navigator/canshare/index.md
{{APIRef("Web Share API")}}{{securecontext_header}}
The canShare() method of the {{domxref("Navigator")}} interface returns true if the equivalent call to {{domxref("navigator.share()")}} would succeed.
The method returns false if the data cannot be validated. Reasons the data might be invalid include:
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.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)
data {{optional_inline}}
: An object defining the share data to test.
Typically, an object with the same properties is passed to {{domxref("navigator.share()")}} if this call returns true.
Properties that are unknown to the user agent are ignored; share data is only assessed on properties understood by the user agent.
All properties are optional but at least one known data property must be specified or the method will return false.
Possible values are:
url {{optional_inline}}
text {{optional_inline}}
title {{optional_inline}}
files {{optional_inline}}
Returns true if the specified data can be shared with {{domxref("Navigator.share()")}}, otherwise false.
The example uses navigator.canShare() to check whether navigator.share() can share the specified data.
The HTML just creates a paragraph in which to display the result of the test.
<p class="result"></p>
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.";
}
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:
{{EmbedLiveSample('Sending_the_MDN_URL')}}
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.
// Feature that may not be supported
let testShare = { someNewProperty: "Data to share" };
// Complex data that uses new key
const shareData = {
title: "MDN",
text: "Learn web development on MDN!",
url: "https://developer.mozilla.org",
someNewProperty: "Data to share",
};
// Test that the key is valid and supported before sharing
if (navigator.canShare(testShare)) {
// Use navigator.share() to share 'shareData'
} else {
// Handle case that new data property can't be shared.
}
{{Specifications}}
{{Compat}}