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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:29:52+00:00 2026-05-25T18:29:52+00:00

Need help! I’ve been looking for a solution for this seemingly simple task but

  • 0

Need help! I’ve been looking for a solution for this seemingly simple task but can’t find an exact one. Anyway, I’m trying to add custom #id to the tag based on the page’s URL. The script I’m using works ok when the URLs are like these below.

- http://localhost.com/index.html
- http://localhost.com/page1.html
- http://localhost.com/page2.html
-> on this level, <body> gets ids like #index, #page1, #page2, etc...

My question is, how can I make the body #id still as #page1 or #page2 even when viewing subpages like this?

- http://localhost.com/page1/subpage1
- http://localhost.com/page2/subpage2

Here’s the JS code I’m using (found online)

$(document).ready(function() {
    var pathname = window.location.pathname; 
    var getLast = pathname.match(/.*\/(.*)$/)[1];
    var truePath = getLast.replace(".html",""); 
            if(truePath === "") { 
                    $("body").attr("id","index");
            }
            else { 
                    $("body").attr("id",truePath);
            }       
    }); 

Thanks in advance!

edit: Thanks for all the replies! Basically I just want to put custom background images on every pages based on their body#id. >> js noob here.

http://localhost.com/page2/subpage2 - > my only problem is how to make the id as #page2 and not #subpage2 on this link.
  • 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-25T18:29:53+00:00Added an answer on May 25, 2026 at 6:29 pm

    Using the javascript split function might be of help here. For example (untested, but the general idea):

    var url = window.location.href.replace(/http[s]?:\/\//, '').replace('.html', '');
    var segments = url.split('/');
    
    $('body').id = segments[0];
    

    Also, you might want to consider using classes instead of ID’s. This way you could assign every segment as a class…

    var url = window.location.href.replace(/http[s]?:\/\//, '').replace('.html', '');
    var segments = url.split('/');
    
    for (var i = 0; i < segments.length; i++) {
        $('body').addClass(segments[i]);
    }
    

    EDIT:

    Glad it worked. Couple of notes if you’re planning on using this for-real: If you ever have an extension besides .html that will get picked up in the class name. You can account for this by changing that replace to a regex…

    var url = window.location.href.replace(/http[s]?:\/\//, '');
    // Trim extension
    url = url.replace(/\.(htm[l]?|asp[x]?|php|jsp)$/,'');
    

    If there will ever be querystrings on the URL you’ll want to filter those out too (this is the one regex I’m not 100% on)…

    url = url.replace(/\?.+$/,'');
    

    Also, it’s a bit inefficient to have the $('body') in every for loop “around” as this causes jQuery to have to re-find the body tag. A more performant way to do this, especially if the sub folders end up 2 or 3 deep would be to find it once, then “cache” it to a variable like so..

    var $body = $('body');
    for ( ... ) {
       $body.addClass( ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need help with this error. Can't see what's wrong. Error: Warning: mysql_query() expects parameter
Need help writing a script downloads data from google insight using c# this is
I need help on this following aspx code aspx Code: <asp:Label ID =lblName runat
I need help with this route map routes.MapRoute(Blog_Archive, Blog/Archive/{year}/{month}/{day}, new { controller = Blog,
i need help with disk_total_space function.. i have this on my code <?php $sql=select
Need help... I have 3 classes, Manager which holds 2 pointers. One to class
I need help with debugging this piece of code. I know the problem is
I need help refactoring this multi-loop thing. Here is what I have: Campaign has_many
Need help with this SQL Server 2000 procedure. The problem is made difficult because
Need help with my simple PHP menu. I'd like to have something like that:

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.