I am really new to JavaScript, but here's what I am starting with:
var anchors = document.body.getElementsByTagName("a");
for(var i=0; i < anchors.length; i++) {
var anc = anchors[i];
if (anc.getAttribute("href") == "/catalog?id=123456789") {
anc.setAttribute("href", "/catalog?id=123456789#specificID");
anc.setAttribute("target", "iframe");
} }
I have a web page with a list of links that are similar to /catalog?id=123456789
where the ID can be any combination of 9 numbers. I want to target all of the links (including the ID), append a page anchor at the end of the link, and then switch them with the existing links on the page. I would be willing to try using JQuery, but the site I am working on uses a MooTools library so it may not work.
Use substr()
to get the beginning of the URL and just match that:
var href = anc.getAttribute("href")
if (href.substr(0, 12) == "/catalog?id=") {
anc.setAttribute("href", href + "#specificID");
anc.setAttribute("target", "iframe");
}