Page is Jumping back to Top after targeting the anchor tag

Hello guys im using wordpress and i need to jump to the comments when the user is clicking a link.
The single.php is opening in a new tab and its loading the page on the anchor (comment-id) but after that its always jumping back to the top of the page.

I Know its a problem with javascript (as its working well when i disable js) but im not sure where to find the peace off javascript i have to stop or to change on single.php.
Has anyone an idea how and where to change the javascript. So that i can stay at the comment after clicking the link on a different page?

Read More

this is the target <a name="comment'.$comment_ID.'" href="#comment'.$comment_ID.'" onclick="deletco('.$comment_ID.')">DELETE</a>

and this is the link form the other page to the target on single.php

<a href="'.$commpostlink.'#comment-'.$commentid.'" target="_blank">'.$titlecomm.'</a> 

thanks for any help

Related posts

Leave a Reply

2 comments

  1. First I encourage you to comment out your JS files one by one it test it out tell you find the responsible js file.

    Another approach is, if its javascript issue, then you can do this with jQuery:

    $('a.aCommentLinkCssClass').click(function () {
      var url = $(this).attr('href').text();
      window.location.href = url;
    });
    

    and add css class to your link:

    <a href="'.$commpostlink.'#comment-'.$commentid.'" target="_blank" class="aCommentLinkCssClass">'.$titlecomm.'</a> 
    

    or if you want just JavaScript only:

    <script>
       function goToFunction(url) {
          window.location.href = url;
       }
    </script>
    

    and in your html:

     <?php echo '<a href="'.$commpostlink.'#comment-'.$commentid.'" onclick="goToFunction("'.$commpostlink.'#comment-'.$commentid.'")" target="_blank">'.$titlecomm.'</a>'; ?>
    
  2. try:

    onclick="deletco('.$comment_ID.'); return false;"
    

    Or inside the function itself add a return false; at the end. You need to prevent bubbling of the event.