'Remove canonical links from dynamically generated pages
I want to remove a canonical tag from my website. It can't be removed directly from the website since the pages are created on the fly and there are hundreds of them. I want to remove them via tag manager but don't know what JS code to write. Can anyone help?
Solution 1:[1]
/*
Here you should select elements you need to remove.
With the querySelectorAll, you will select the elements you want to remove.
Assume that we want to remove li elements with .red class in this example.
liList is a nodeList. So you can use foreach for iterating.
*/
let liList = document.querySelectorAll('.red');
liList.forEach((currentLi) => {
currentLi.remove(); //Removing all selected li elements
});
<html>
<body>
<ul>
<li class="red">List1</li>
<p>Text1</p>
<li class="red">List2</li>
<p>Text2</p>
<li class="red">List3</li>
<p>Text3</p>
<li class="red">List4</li>
<p>Text4</p>
<li class="red">List5</li>
</ul>
</body>
</html>
What you need to do is to select the tags you want to remove with DOM operators. I leave a simple example here. All you have to do is make a querySelectorAll operation that suits your needs.
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 |
