Javascript add parameter if “ccm_order_by_direction=desc” is in the URL

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>