The code appends the page URL after clicking specific links on the page. When I run the code, it performs as expected, appending the URL only to links that match the code. However, when I print to the console, I noticed that it appeared to do this for every single link on the page, despite the if statement that was supposed to limit it. When I took the if statement out, the code functioned the same. However, even though it works, I want to make it more efficient and only run on the links that match the parameters.
// Function to change the link by adding the current page
function setSlateLink() {
// get the URL of the current page
var currentPage = document.location.href;
// get the URL of the slate
var slateLink = jQuery('a[href$="code=magic"]').attr("href");
// rewrite the slate, adding the query string with a key for Slate and value of the current page
console.log("Processing link change");
return slateLink += "&sys:interaction:summary=" + currentPage;
}
jQuery(document).ready(function() {
jQuery("a").each(function(i, item) {
// if there is an a tag, check that it ends like this
if (jQuery(i == 'a[href$="code=magic"]')) {
console.log("Found link: " + jQuery("a").attr("href"));
// change the link to set the referrer (current page) in the URL
jQuery('a[href$="magic"]').attr("href", setSlateLink());
console.log("Changed link");
}
});
});
Any idea what would cause this?