skip to Main Content

I am trying to find a way to add &#filterTop to the end of the URL if ccm_order_by_direction=desc is in the URL.

I assume JavaScript would be the easiest, but if there is a PHP way that would work too.

The catch is that it would have to update the existing page link. The links are created in the backend and I can not edit them directly. I know I did something like this before, I can not remember how or where the info would be.

I got this far. I just don’t know where to go next.

$(document).ready(function() {
  if (window.location.href.indexOf('ccm_order_by_direction=desc') !== -1) {
    let url = '<?= $actual_link ?>';
    url = url.replace('ccm_order_by_direction=desc', 'ccm_order_by_direction=desc&amp;#filterTop');
    var link = document.getElementsByClassName('.page-link');
    link.href = url;
  }
});

The links that needs updated look like this.

<li class="page-item">
  <a class="page-link" href="/switches-damper-arm-tilt-tip-over-limit?ccm_paging_p=2&amp;ccm_order_by=popular&amp;ccm_order_by_direction=desc">2</a>
</li>

I would like them to look like.

<li class="page-item">
  <a class="page-link" href="/switches-damper-arm-tilt-tip-over-limit?ccm_paging_p=2&amp;ccm_order_by=popular&amp;ccm_order_by_direction=desc&amp;#filterTop">2</a>
</li>

2

Answers


  1. Chosen as BEST ANSWER

    let testUrl = new URL('https://www.mytinytown.com/switches-damper-arm-tilt-tip-over-limit?ccm_paging_p=2&amp;ccm_order_by=popular&amp;ccm_order_by_direction=desc');
    
    // In your case:
    // let testUrl = $(location).attr('href');
    
    $(document).ready(function() {
      let params = new URLSearchParams(testUrl.search);
      if (params.get("ccm_order_by_direction") === "desc") {
        // change the one you want
        params.set("ccm_order_by_direction", "desc&#filterTop");
        let newURL = testUrl.href + "?" + decodeURIComponent(params.toString())
        $('.page-link').attr('href', newURL);
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <li class="page-item">
      <a class="page-link" href="https://www.mytinytown.com/switches-damper-arm-tilt-tip-over-limit?ccm_paging_p=2&amp;ccm_order_by=popular&amp;ccm_order_by_direction=desc">Change Test</a>
    </li>


  2. Without troubleshooting all your code, I can see right off the bat this line won’t work:

    var link = document.getElementsByClassName('.page-link');
    link.href = url;
    

    For that, you’d need to iterate – in which case I recommend using querySelectorAll (which will return an iterable collection list) which you can then manipulate in a forEach loop – and change the links one by one, but you’re using jQ so you can simply do:

    $('.page-link').attr('href', newURL);
    

    Which (considerately) will take care of it. However, I wanted to explore how to do this without string manipulation, so I have a solution here that uses URLSearchParams to objectify the query string and then allow you to use the baked in methods to replace the item in question. I use decodeURIComponent at the end since the URLSearchParams will decode the symbols (? #) into %d4 or whatever.

    let testUrl = new URL('https://this-example.com?ccm_order_by_direction=desc');
    
    // In your case:
    // let testUrl = $(location).attr('href');
    
    $(document).ready(function() {
      let params = new URLSearchParams(testUrl.search);
      if (params.get("ccm_order_by_direction") === "desc") {
        // change the one you want
        params.set("ccm_order_by_direction", "desc&#filterTop");
        let newURL = testUrl.href + "?" + decodeURIComponent(params.toString())
        $('.page-link').attr('href', newURL);
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <ul>
      <li class="page-item">
        <a class="page-link" href="/switches-damper-arm-tilt-tip-over-limit?ccm_paging_p=2&amp;ccm_order_by=popular&amp;ccm_order_by_direction=desc">Inspect this link</a>
      </li>
      <li>
       <a class="page-link" href="/switches-damper-arm-tilt-tip-over-limit?ccm_paging_p=2&amp;ccm_order_by=popular&amp;ccm_order_by_direction=desc">Inspect this link too</a>
      </li>
      
    </ul>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search