'How can I exclude this JS code from running on certain URL strings?
I have some JS code (Swarmify) running on every page of my website (mydomain.com) that changes the skin of YouTube videos (to Swarmify).
I can modify this code in the parent site dashboard.
I want to exclude it from running on certain pages (mydomain.com/blog*), by editing the parent code; i.e. I want all my blog videos to be simply embedded YouTube videos without needing to edit each individual blog post.
I don't know JS at all.
Is there a way/command I can insert that will add the URL string exception? (and using a wildcard; i.e. all pages that begin mydomain.com/blog*)
Here is the code (the cdnkey has been amended : )).
<!-- start Swarmify JS code-->
<script data-cfasync="false">
var swarmoptions = {
swarmcdnkey: "myrandomstring",
iframeReplacement: "iframe",
autoreplace: {
youtube: true
},
theme: {
primaryColor: "#eb6a56"
}
};
</script>
<script async data-cfasync="false" src="https://assets.swarmcdn.com/cross/swarmdetect.js"></script>
<!-- end Swarmify JS code-->
I contacted the developer but they were unable to help.
As per this post answer, I added the suggested JS script (see below) which stopped Swarmify running (good!), but stopped it on all pages of my website, not just blog pages (bad!).
Perhaps I didn't add that code in the right way?
<script data-cfasync="false">
if (window.location.href.indexOf('/blog*') != -1) {
//don't run on blog pages
return;
}
var swarmoptions = {
swarmcdnkey: "myrandomstring",
iframeReplacement: "iframe",
autoreplace: {
youtube: true
},
theme: {
primaryColor: "#eb6a56"
}
};
</script>
<script async data-cfasync="false" src="https://assets.swarmcdn.com/cross/swarmdetect.js"></script>
Solution 1:[1]
You can try the below code. I hope it will solve your problem.
<script data-cfasync="false">
var swarmoptions = {};
window.addEventListener('DOMContentLoaded', (event) => {
if (/\/blog[.]*/ig.test(window.location.toString())) {
return;
}
swarmoptions = {
swarmcdnkey: "myrandomstring",
iframeReplacement: "iframe",
autoreplace: {
youtube: true
},
theme: {
primaryColor: "#eb6a56"
}
};
});
</script>
<script async data-cfasync="false" src="https://assets.swarmcdn.com/cross/swarmdetect.js"></script>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
