Last active
April 16, 2024 08:26
-
-
Save joychetry/79607fcf004dc01d6fc93517f36d8242 to your computer and use it in GitHub Desktop.
Prompt native sharing options inside a post loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
document.addEventListener('DOMContentLoaded', function() { | |
// Click event listener for images with class 'shareLink' | |
const images = document.querySelectorAll('.shareLink'); | |
images.forEach(function(image) { | |
image.addEventListener('click', function() { | |
const link = this.getAttribute('data-link'); | |
const postTitle = this.getAttribute('data-title'); // Assuming 'data-title' attribute contains the post title | |
// Check if Web Share API is supported | |
if (navigator.share) { | |
navigator.share({ | |
title: postTitle, | |
text: 'Check this out:', | |
url: link, | |
}).catch((error) => { | |
console.error('Error sharing link:', error); | |
}); | |
} else { | |
// Fallback to copying link to clipboard for unsupported browsers | |
const textarea = document.createElement('textarea'); | |
textarea.value = link; | |
document.body.appendChild(textarea); | |
textarea.select(); | |
document.execCommand('copy'); | |
document.body.removeChild(textarea); | |
alert('Link copied to clipboard: ' + link); | |
} | |
}); | |
}); | |
}); | |
</script> | |
//Copy Paste this to Bricks Builder | |
{"content":[{"id":"ynnvqz","name":"image","parent":"oregbp","children":[],"settings":{"image":{"id":1395,"filename":"doo-03.svg","size":"full","full":"https://fab.8masons.co/wp-content/uploads/doo-03.svg","url":"https://fab.8masons.co/wp-content/uploads/doo-03.svg"},"_cssGlobalClasses":["kbohre"],"_attributes":[{"id":"kcmodh","name":"data-link","value":"{post_url}"},{"id":"wdhpge","name":"data-title","value":"{post_title}"}]},"themeStyles":{}}],"source":"bricksCopiedElements","sourceUrl":"https://fab.8masons.co","version":"1.9.7.1","globalClasses":[{"id":"kbohre","name":"shareLink","settings":{"_cursor":"pointer"}}],"globalElements":[]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment