Skip to content

Instantly share code, notes, and snippets.

<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>
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
<!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>
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>
<button id="share-button">Share</button>
</body>
<script src='share.js'></script>
</html>
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
// Web Share APIs are provided by navigator.share method
if (navigator.share) {
console.log("Web Share APIs are supported")
} else {
console.log("Web Share APIs are not supported")
}
var log = console.log
log("Inside global execution context")
function functionOne() {
log("Inside function one")
function setTimeoutFunction() {
log("Inside setTimeoutFunction: I will be executed atleast after 1 sec")
}
var timerId = setTimeout(function (){
console.log("This function will be removed before it's executed")
}, 100)
clearTimeout(timerId)
// No output will be displayed as the setTimeout callback function
// will be cancelled before it will be executed
function multiplyByTwo (num) {
console.log(num + " multiplied by 2 is " + num*2)
}
// Syntax - setTimeout(callbackMethod, delay, param1, param2, ..)
setTimeout(multiplyByTwo, 100, 4)
// Output:
// 4 multiplied by 2 is 8