0

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?

2
  • 3
    your if will always be true, because it evaluates to an empty jQuery collection Commented Jul 1, 2015 at 19:43
  • 1
    Code makes no sense, you are selecting the magic links on every iteration of the loop. SO you keep doing it over and over again to the same links Commented Jul 1, 2015 at 19:48

2 Answers 2

4

you calling if (jQuery(i == 'a[href$="code=magic"]'))
this returns a jquery object which is truthy, causing the if statement to always execute.

if youre trying to see if that element matches the selector, you can use jquerys #is method:
if ($(item).is('a[href$="code=magic"]'))

Sign up to request clarification or add additional context in comments.

1 Comment

You should also provide a solution ;)
2

Use is() to determine if an element would be matched by a selector. http://api.jquery.com/is/

Example:

if ($(item).is('a[href$="code=magic"]')) {
  // item matches
}

But you could simplify it by selecting those specific a tags to start with:

$('a[href$="code=magic"]').each(function(i, item) {
  // just code=magic links...
});

Going one step further...

$('a[href$="code=magic"]').each(function(i, item) {
  var href = $(item).attr('href');

  $(item).attr('href', href + '&sys:interaction:summary=' + document.location.href);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.