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 6540353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:58:05+00:00 2026-05-25T10:58:05+00:00

I have a script on http://joelglovier.com to add a class of active to each

  • 0

I have a script on http://joelglovier.com to add a class of “active” to each navigation element when it’s corresponding section in the document. The script is adapted with permission from w3fools.com, so it was written without my scenario in mind.

The difference is that on w3fools.com, the nav links only refer to elements on the page, whereas in my navigation, there is one element at the end that refers to a new page.

The problem is that as I have adapted it, it works fine for the links that refer to sections on the page. However, for some reason unbeknownst to me (sorry – JS/jQuery novice) it is blocking the browser from following the link to the new page (the Blog link).

I tried reading through the code and understanding what the script is doing – however I cannot seem to understand why it is blocking that external link from being clicked, or more specifically how to fix it.

Can anybody suggest the simplest way to remedy my issue without breaking the original functionality of the script for it’s purpose?

See it live here: http://joelglovier.com

Or…

Markup:

<div id="site-nav">

<div class="wrap">

    <nav id="nav-links">

            <a href="#jag-home" class="section-link scroll top">Top</a>

            <a href="#background-section" class="section-link scroll skills">Background</a>

            <a href="#projects-section" class="section-link scroll projects">Projects</a>

            <a href="#random-section" class="section-link scroll random">Random</a>

            <a href="#site-footer" class="section-link scroll credits">Credits</a>

            <a href="http://blog.joelglovier.com" class="section-link blog" title="my tumblr">Blog <span class="icon"></span></a>

    </nav>

</div>

Javascript:

(function($) {
  $(function() {

    var $nav = $('#nav-links'),
        $navLinks = $nav.find('a.scroll'),
        cache = {};
        $docEl = $( document.documentElement ),
        $body = $( document.body ),
        $window = $( window ),
        $scrollable = $body; // default scrollable thingy, which'll be body or docEl (html) 

    // find out what the hell to scroll ( html or body )
    // its like we can already tell - spooky
    if ( $docEl.scrollTop() ) {
        $scrollable = $docEl;
    } else {
        var bodyST = $body.scrollTop();
        // if scrolling the body doesn't do anything
        if ( $body.scrollTop( bodyST + 1 ).scrollTop() == bodyST) {
            $scrollable = $docEl;
        } else {
            // we actually scrolled, so, er, undo it
            $body.scrollTop( bodyST - 1 );
        }
    }       

    // build cache
    $navLinks.each(function(i,v) {
        var href =  $( this ).attr( 'href' ),
            $target = $( href );
        if ( $target.length ) {
            cache[ this.href ] = { link: $(v), target: $target };
        }
    });

    // handle nav links
    $nav.delegate( 'a', 'click', function(e) {
    //  alert( $scrollable.scrollTop() );
        e.preventDefault(); // if you expected return false, *sigh*
        if ( cache[ this.href ] && cache[ this.href ].target ) {
            $scrollable.animate( { scrollTop: cache[ this.href ].target.position().top }, 600, 'swing' );
        }
    });

    // auto highlight nav links depending on doc position
    var deferred = false,
        timeout = false, // so gonna clear this later, you have NO idea
        last = false, // makes sure the previous link gets un-activated
        check = function() {
            var scroll = $scrollable.scrollTop(),
                height = $body.height(),
                tolerance = $window.height() * ( scroll / height );

            $.each( cache, function( i, v ) {
                // if we're past the link's section, activate it
                if ( scroll + tolerance >  v.target.position().top  ) {
                    last && last.removeClass('active');
                    last = v.link.addClass('active');
                } else {
                    v.link.removeClass('active');
                    return false; // get outta this $.each
                }
            });

            // all done
            clearTimeout( timeout );
            deferred = false;
        };

    // work on scroll, but debounced
    var $document = $(document).scroll( function() {
        // timeout hasn't been created yet
        if ( !deferred ) {
            timeout = setTimeout( check , 250 ); // defer this stuff
            deferred = true;
        }
    });

    // fix any possible failed scroll events and fix the nav automatically
    (function() {
        $document.scroll();
        setTimeout( arguments.callee, 1500 );
    })(); 

});

})(jQuery);

  • 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-05-25T10:58:06+00:00Added an answer on May 25, 2026 at 10:58 am

    You’re trying to tell it to scroll to “http://…” which doesn’t exist on the current page, so it fails and does nothing.

    It should work if you change your code to this

    // handle nav links
    $nav.delegate( 'a', 'click', function(e) {
    //  alert( $scrollable.scrollTop() );
        e.preventDefault(); // if you expected return false, *sigh*
        if ( cache[ this.href ] && cache[ this.href ].target ) {
            // preserve http:// links
            if (this.href.substr(0, "http://".length) == 'http://')
                location.href = this.href;
            else
                $scrollable.animate( { scrollTop: cache[ this.href ].target.position().top }, 600, 'swing' );
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this: script_url = 'http://externaldomain.com/script.js' div_id = 'div'+Math.floor(Math.random() * 1000000); document.write('<div id='+div_id+'><scr'+'ipt id=banner-to-load></scr'+'ipt></div>')
We have script http://site.com/ourscript.php How to add something like enter password block on that
I have a bash script mystuff containing a line like lynx -dump http://example.com >tmpfile
Right now I have a script thats http://www.example.com/cgi-bin/foo?var1=A&var2=B Is there a way that I
I have the following script from http://javascript.about.com/library/bljver.htm <script type=text/javascript> var jsver = 1.0; </script>
I have an activex plugin here: http://reboltutorial.com/plugins/logo-badge/ I tried by adapting the script http://forums.devarticles.com/javascript-development-22/detecting-activex-objects-installed-in-ie-11041.html
Hi i have this script http://jsbin.com/ajaneh , when i make an action throw ajax
I have this script to pick a date in a form: <script src=http://trentrichardson.com/examples/timepicker/js/jquery-ui-timepicker-addon.js></script> <script>
I have been using this script ( http://www.dynamicdrive.com/dynamicindex6/dhtmlcount.htm ) for countdown to vacations. But
I have a php script http://example.com/myapp/myscript.php running on an Apache server. I call this

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.