Run some script on content in the same web part zone or wiki content area
This code lets you reference a script one or more times on a page, have that code detect where it is located on the page, and then do something with the other content in that same zone
<script type="text/javascript"> var detectScriptRefFolderUrl = "_spPageContextInfo.siteAbsoluteUrl +"/Style%20Library/"", detectScriptRefFileName = "detectWPZ-vanilla.js"; var detectScriptRefDict = detectScriptRefDict || []; var fxAppendDetectWPZScript = fxAppendDetectWPZScript || function(){ var appendScriptTag = document.createElement("SCRIPT"); appendScriptTag.src = + detectScriptRefFileName; // make a unique ID based on the datetimestamp in JS milliseconds for this instance of this script reference var thisMoment = new Date(); appendScriptTag.setAttribute("instanceMoment", thisMoment.valueOf()); document.writeln(appendScriptTag.outerHTML); var bFirstInt = true; var myInt = setInterval(function(){ if ( bFirstInt === true ){ if ( !document.querySelector("[script*='/Style%20Library/"+ detectScriptRefFileName +"']") ){ var appendScriptTag = document.createElement("SCRIPT"); appendScriptTag.src = detectScriptRefFolderUrl + detectScriptRefFileName; document.head.appendChild(appendScriptTag); } } if ( typeof(detectWPZ) !== "undefined" ){ var dictIndex = detectScriptRefDict.push(new wpzDetector(thisMoment.valueOf())); dictIndex--; detectScriptRefDict[dictIndex].scriptTag = document.querySelector("SCRIPT[src*='detectWPZ'][instanceMoment='"+detectScriptRefDict[dictIndex].instanceName+"']"); detectScriptRefDict[dictIndex].guid = detectScriptRefDict[dictIndex].scriptTag.parentElement.parentElement.getAttribute("webpartid"); detectScriptRefDict[dictIndex].myWebPart = detectScriptRefDict[dictIndex].scriptTag.parentElement.parentElement.parentElement.parentElement; detectScriptRefDict[dictIndex].wpz = parentsUntilMatchingSelector(detectScriptRefDict[dictIndex].myWebPart,"TD[name='_invisibleIfEmpty'], DIV.ms-rte-layoutszone-inner", 50); if ( detectWPZ.isPageInEditMode() === false ) { detectWPZ.getZoneWebParts(detectScriptRefDict[dictIndex], function(siblingWebPart){ try{console.log("wpzDetector instance "+ thisMoment.valueOf());}catch(err){} try{console.log(siblingWebPart);}catch(err){} }, function(){ try{console.log("wpzDetector instance "+ thisMoment.valueOf() +" finished looping through my WPZ's web parts");}catch(err){} setTimeout(function(){ if ( typeof(convertDetectedWebPartsToTabs) !== "undefined" ){ convertDetectedWebPartsToTabs(); } },500); }); } var collWPs = detectScriptRefDict[dictIndex].wpz.querySelectorAll("[id^='MSOZoneCell_WebPartWPQ']"); for ( var iSibling = 0; iSibling < collWPs.length; iSibling++ ){ var siblingWebPart = collWPs[iSibling]; if ( siblingWebPart.scrollWidth > detectScriptRefDict[dictIndex].fixedWidth ) { detectScriptRefDict[dictIndex].fixedWidth = siblingWebPart.scrollWidth; } } clearInterval(myInt); } bFirstInt = false; },756); }; function parentsUntilMatchingSelector(element, selector, iBreakAt) { if (typeof(selector) === "undefined") { var selector = ".ms-webpartzone-cell"; } if (typeof(iBreakAt) === "undefined") { var iBreakAt = 100; } var iBreak = 0; var parent = element.parentElement; while (parent.querySelectorAll(selector).length < 1 && iBreak < iBreakAt) { parent = parent.parentElement; iBreak++; } if (parent.querySelectorAll(selector).length > 0) { var coll = parent.querySelectorAll(selector); for (var iWPZ = 0; iWPZ < coll.length; iWPZ++) { var wpz = coll[iWPZ]; if (wpz.querySelectorAll("#" + element.id).length > 0) { try { console.log(wpz); } catch (err) {} parent = wpz; break; } } return parent; } else { return false; } }; fxAppendDetectWPZScript(); </script>