Scenario
- You have a promoted tiles web part on a page
- You need to prompt the user with a dialog box prior to navigating to the link on click
Issue
- The onclick attribute is dynamically populated by the control, so it's hard to manipulate using Javascript or JQuery
Resolution
- The key is to use the mousedown event instead.
- Note: This will not support keyboard navigation so you may need to handle the keydown event as well.
- Here is the code for the Script Editor. Adapt the URLs for your environment
<script type="text/javascript" src="/SiteAssets/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="/teams/print/SiteAssets/PromotedLinkPrompt.js"></script>
- Here is the PromotedLinkPrompt.js code
console.log("PromotedLinkPrompt.js Loaded");
localStorage.setItem("PopupContentDone", "false");
function PopupContent()
{
var done = localStorage.getItem("PopupContentDone");
if (done == "false")
{
if (confirm("Does this work?\n\nClick OK to Proceed.\nClick Cancel for More Info."))
{
$(this).find("a")[0].click();
}
else
{
window.location = "http://www.votematrix.com";
}
localStorage.setItem("PopupContentDone", "true");
}
}
$(document).ready(function () {$("div .ms-tileview-tile-content").mousedown(PopupContent)});