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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:08:51+00:00 2026-05-26T03:08:51+00:00

a javascript+DOM question. Can someone improve my code? I have a page nav bar

  • 0

a javascript+DOM question.

Can someone improve my code?

I have a page nav bar like this (on some pages):

<div id="pagebar">
<a href="iraq_pixra1.html">1</a>
<a href="iraq_pixra2.html">2</a>
<a href="iraq_pixra3.html">3</a>
<a href="iraq_pixra4.html">4</a>
<a href="iraq_pixra5.html">5</a>
<a href="iraq_pixra6.html">6</a>
</div>

I want my javascript to change the link style so that the style of the current page is highlighted.

My javascript code is this. It finds the current page’s url, then try to match it with one of the link tag. If match, then set style.

function setPageBarStyle() {
    var pageNavBar = document.getElementById("pagebar");

    if (pageNavBar != null) {
        var fPath = document.location.pathname;
        var fPathParts = fPath.split('/');
        var fName = (fPathParts[fPathParts.length-1]); // file name after last slash. e.g. xyz.html

        var linkTags = pageNavBar.getElementsByTagName("a");
        for (var ii = 0; ii < linkTags.length; ii++) {
            var aUrl = linkTags[ii].href;
            var aUrlParts = aUrl.split("/");
            var aUrlLastPart = (aUrlParts[aUrlParts.length-1]); // part after last slash. e.g. xyz.html
            if (aUrlLastPart == fName) { 
                linkTags[ii].style.border="thin solid blue";
            }
        }
    }
}

setPageBarStyle();

How can i improve the code? I am new to DOM scripting.

The way i split the path seem too verbose, with many variables. I think it can be done with regex, but don’t know how with javascript/DOM.

Thanks.

  • 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-26T03:08:51+00:00Added an answer on May 26, 2026 at 3:08 am

    You can change this:

    var aUrl = linkTags[ii].href;
    var aUrlParts = aUrl.split("/");
    var aUrlLastPart = (aUrlParts[aUrlParts.length-1]); // part before last slash. e.g. xyz.html
    

    With this:

      var aUrlLastPart = linkTags[ii].href.split("/").pop();
    

    The Pop() method will return the last element of the array returned by split() and remove it from the array (which in your case, is irrelevant).

    I would also recommend checking out jQuery or similars, in order to manage cross browser JS easier…

    Let me know if it helps! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Premise: I've started to study javascript and the DOM and I have this HTML
Recently I posed a question , asking why some of my javascript code was
Let me give my question some context. I have a JavaScript widget. The widget
I assume this one is rather a JavaScript/jQuery/DOM question than a Rails question, but
I'm having the weird problem that after having javascript inject some dom-elements the css-rules
I have a javascript function that manipulates the DOM when it is called (adds
My DOM skills are weak and I can't work out why a javascript variable
I am trying to write DOM viewer on JavaScript. How I can get all
I'm looking for a DOM event that I can listen to with JavaScript for
How can you reliably and dynamically load a JavaScript file? This will can be

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.