Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8724285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:47:54+00:00 2026-06-13T07:47:54+00:00

I am using the following jquery script on my site to scroll down on

  • 0

I am using the following jquery script on my site to scroll down on blog entry pages from a comments link at the top to the actual comments at the bottom:

   jQuery(document).ready(function(){      
       var TopPosition = jQuery('#comments').offset().top;     
       jQuery('.comments-link').click(function(){        
           jQuery('html, body').animate({scrollTop:TopPosition}, 2000, 'swing');
           return false;
       });   
   });

I’d also like the page to scroll down to “#respond’ when ‘#comments’ does not exist. Is this possible somehow in jquery with an if/else/and?

Also, is it possible to link from a different page with a comments link (i.e. my blog’s index page) to the individual blog entry’s comments and also have the animated scroll down?

Hope that makes sense.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:47:56+00:00Added an answer on June 13, 2026 at 7:47 am

    If you want to scroll to #respond when #comments doesn’t exist, just check the length of the object returned by the selector… if it is 0 then it’s not on the page, and you can use the offset().top of #respond instead.

    jQuery(document).ready(function(){
       var comments = jQuery('#comments');
       var TopPosition = (comments.length==0)? 
           jQuery('#respond').offset().top :
           comments.offset().top;     
       jQuery('.comments-link').click(function(){        
           jQuery('html, body').animate({scrollTop:TopPosition}, 2000, 'swing');
           return false;
       });   
    });
    

    To scroll down on page load, you would need to use a hash (for example page.php#comments), however traditionally you would have an anchor tag (<a name="comments'></a>), and the page would just jump to it. If you want to have a smooth scroll, you can look at location.hash and then trigger the scroll. Since it is effectively the same as the onClick I would break it out into a function that you can call for both:

    jQuery(document).ready(function(){
        // Set up the onClick() event
        jQuery('.comments-link').click(scrollToComments);
    
        // If the page is page.php#comments scroll to the comments/response
        if (location.hash=='#comments') scrollToComments();
    });
    
    // This function handles the scrolling on page load and onclick
    function scrollToComments(){
        var comments = jQuery('#comments');
        // this can be moved outside the function, or recalculate in case the page redraws
        var scrollTopPosition = (comments.length==0)? 
           jQuery('#respond').offset().top :
           comments.offset().top;
        jQuery('html, body').animate({scrollTop:scrollTopPosition}, 2000, 'swing');
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the following script to to load pages of my site: var
I am using JQuery with the Star Rating Plugin from the following site http://www.fyneworks.com/jquery/star-rating/#
I'm using the following jQuery plugins (along with the jQuery plugin): <script type=text/javascript src=scripts/jquery.scrollTo-min.js></script>
I've got the following jsp page and I'm using struts2-jquery-plugin-3.1.1 <script type=text/javascript> function openDialog()
I'm using the following JQUERY Autocomplete plug-in: jQuery Plugin: Tokenizing Autocomplete Text Entry What'd
how can i parse the particular 'li' field from the following using jquery template?
I have the following jQuery script on my site to switch tabs without reloading
This is regarding the following jQuery plugin: http://blog.danawoodman.com/jquery-showhide/ I am using jQuery show hide
I'm using jQuery Raty and I have the following script: <script type=text/javascript charset=utf-8 src={{
I am using the following sliding div script: http://www.webdesignerwall.com/demo/jquery/simple-slide-panel.html Currently, the slidetoggle function is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.