Skip to content

Instantly share code, notes, and snippets.

var shareButton = document.getElementById('share-button');
shareButton.addEventListener('click', function () {
// Check if navigator.share is supported by the browser
if (navigator.share) {
console.log("Congrats! Your browser supports Web Share API")
// navigator.share accepts objects which must have atleast title, text or
// url. Any text or title or text is possible
<!DOCTYPE html>
<html>
<body>
<button id="share-button">Share</button>
</body>
<script src='share.js'></script>
</html>
var shareData = { files: filesArray };
if (navigator.canShare && navigator.canShare(shareData)) {
// Share the data.
} else {
console.log("Your system doesn't support sharing files.");
}
var shareButton = document.getElementById('share-button');
shareButton.addEventListener('click', function () {
var filesArray = document.getElementById('share-files').files
var shareData = { files: filesArray };
if (navigator.canShare && navigator.canShare(shareData)) {
// Adding title afterwards as navigator.canShare just
// takes files as input
var shareData = { files: filesArray };
if (navigator.canShare && navigator.canShare(shareData)) {
// Adding title afterwards as navigator.canShare just
// takes files as input
shareData.title = "Bits and pieces"
navigator.share(shareData)
.then(() => console.log('Share was successful.'))
.catch((error) => console.log('Sharing failed', error));
<!DOCTYPE html>
<html>
<body>
<label for=shared_file>File(s):</label></td>
<input id='share-files' type="file" multiple>
<button id="share-button">Share</button>
</body>
<script src='share.js'></script>
</html>
<html>
<body>
<label>Search</label>
<!-- Renders an HTML input box -->
<input type="text" id="search-box" oninput="makeAPICall()">
<!-- Displays number of time makeAPICall method is called -->
<p id='show-api-call-count'></p>
</body>
<html>
<body>
<button id="click-btn">Click me!</button>
</body>
<script>
var timerId;
var clickBtn = document.getElementById('click-btn');
var clickFunction = function(){
function delayFuncExec() {
console.log("I will be called after 100 milliseconds");
}
var timerId = setTimeout(delayFuncExec, 100)
console.log("Timer Id: " + timerId)
function delayFuncExec () {
console.log("I will be called after every 100 milliseconds");
}
var timerId = setInterval(delayFuncExec, 100)
console.log("Timer Id: " + timerId)