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

  • Home
  • SEARCH
  • 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 6729709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:19:22+00:00 2026-05-26T10:19:22+00:00

So I’m trying to put together a navigation bar and I’ve been using PHP

  • 0

So I’m trying to put together a navigation bar and I’ve been using PHP because I’m not familiar with Javascript. I want to create a function that will return the filename of the current page I’m on and then apply css classes appropriately.

the php code

function setNav($section)
{
    $curSection = end(explode('/', $_SERVER['SCRIPT_NAME']));
    if ($section == $curSection)
    {
        echo ' class="active" ';
    }
}

And then I would just initialize it in the html as follows

<a href="abc.def" <?php setNav('index.php'); ?> >Home </a>

For obvious reasons I’d rather do this in JavaScript or Jquery, but I’m having some trouble putting together the function. I’ve looked at a few tutorials, but they leave out a few things.

  1. How to pull just the last part of the file name. I’ve seen a couple ideas doing something like:

    <script type="text/javascript">
        var url = document.location.href;
        url = url.substring(0, (url.indexOf("#") == -1) ? url.length : url.indexOf("#"));
        url = url.substring(0, (url.indexOf("?") == -1) ? url.length : url.indexOf("?"));
        url = url.substring(url.lastIndexOf("/") + 1, url.length);
        alert(url);
    </script>
    

    But then I run into problem 2. Also I understand how substring works but I have no idea what he’s doing w/ the second argument for example ‘(url.indexOf(“#”) == -1) ? url.length : url.indexOf(“#”)’ I searched google, w3schools and a couple places for an explanation, also looked into advanced usage of substring() and nothing came up. Any guidance is appreciated.

  2. If say the file is the index, the url looks something like localhost/abc_corp/ so doing a document.URL call brings up “localhost/abc_corp/” and leaves out the index.php part.

Any help is appreciated in advance.

  • 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-26T10:19:22+00:00Added an answer on May 26, 2026 at 10:19 am

    If you have a working PHP solution I’m not sure why you want to convert it to JavaScript. PHP is a better place to do it anyway.

    Anyway, to explain what your JS is doing, if you have a URL like this:

    http://www.somedomain.com/somepage.php?someparam=234#123123

    it will throw away anything after the “?” and/or “#”, and anything before the last “/”, thus keeping just the bit that I put in bold.

    If your URL does not contain any “/” characters, e.g., you just have “www.somedomain.com”, then it will return the whole string, it can’t invent a filename for you.

    How this works: first note that aString.indexOf(param) returns -1 if aSring does not contain param, otherwise it returns the index of the first instance. So the second argument to substring is being set to one of two values depending on what indexOf finds. The following:

    var url = document.location.href;
    url = url.substring(0, (url.indexOf("#") == -1) ? url.length : url.indexOf("#"));
    

    starts with the full URL, then it checks whether there is a “#” character – if there is not then the substring takes the whole string (0 through to url.length), otherwise it takes the string up to but not including the “#”. Then:

    url = url.substring(0, (url.indexOf("?") == -1) ? url.length : url.indexOf("?"));
    

    Same idea, but with “?”. If there isn’t one keep the whole string, otherwise keep everything up to but not including the “?”. Then:

    url = url.substring(url.lastIndexOf("/") + 1, url.length);
    

    Takes everything from the character after the last “/” (which will be the first character (“-1 + 1”) if there are none) through to the end of the string.

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

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to loop through a bunch of documents I have to put
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.