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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:09:33+00:00 2026-05-22T00:09:33+00:00

What I’m trying to achieve: Based on URL (ie., foo.com/item1), the div element logoswap

  • 0

What I’m trying to achieve:
Based on URL (ie., foo.com/item1), the div element “logoswap” receives a different class.

The following is the code I put together but it seems completely wrong. I’m not a JS pro by any means, XHTML/CSS is more my speed (some PHP)… I cannot use PHP, even if it is possible in PHP (and I know it is because I have a PHP version of what I need done already, but I can’t call the PHP properly.

I’m really just trying to get a different logo to show up based on the directory/url… It doesn’t have to be a background element called in by the CSS class necessarily, I just need a different image to load based on the aforementioned url variable…

  $(function() {
  var url = location.pathname;

  if(url.indexOf('item1') > -1) {
    document.getElementById("logoswap").className += " class1";
   }

  elseif(url.indexOf('item2') > -1) {
    document.getElementById("logoswap").className += "class2";
   }

  elseif(url.indexOf('item3') > -1) {
    document.getElementById("logoswap").className += "class3";
   }

  elseif(url.indexOf('item4') > -1) {
    document.getElementById("logoswap").className += "class4";
   }

  elseif(url.indexOf('item5') > -1) {
    document.getElementById("logoswap").className += "class5";
   }

  else {
    document.getElementById("logoswap").className += "class1";
   }

  });

That’s what I have… Ugly I’m sure.

That’s why I’m here though, I definitely need some help.

  • 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-22T00:09:34+00:00Added an answer on May 22, 2026 at 12:09 am

    Assigning CSS Class By URL Pathname

    A jsfiddle has been setup for
    this solution.

    Here is a case for using numeric expressions if they are available. This does not apply to the above question.

    $(function() {
      var rgx = /item(\d+)$/,
          url = location.pathname,
          id = (rgx.test(url)) ? url.match(rgx)[1] : '1';
      $("#logoswap").addClass("class" + id);
    });
    

    UPDATE:

    In light of the new details you may need an array of values, these should be derived from or exactly equal to the class names you intend to use.

    $(function(){
      // my favorite way to make string arrays.
      var matches = "brand1 brand2 brand3".split(" "),
          url = location.pathname.match(/\w+$/)[0], // get the last item
          id = matches.indexOf(url),
          className = matches[(id > -1) ? id : 0];
      $("#logoswap").addClass(className);
    });
    

    To make this work you will need a few things in place. I will assume that the paths will end in a number as we have outlined here. The default ends with 1. You will need the images to be accessible. You need to define the styles for each possibility.


    CSS Setup

    #logoswap {
      height : 200px;
      width : 200px;
    }
    .class1 {
      background-image : url(/path/to/default.jpg);
    }
    .class2 {
      background-image : url(/path/to/second.jpg);
    }
    .brand1 {
      background-image : url(/path/to/brand/1/logo.jpg);
    }
    ...
    

    Without jQuery

    if you do not have jQuery in your code you may need to use window.onload.

    (function(){
      var old = window.onload;
      window.onload = function(){
        old();
        var r = /item(\d+)$/,
            url = location.pathname,
            id = (r.test(url)) ? url.match(r)[1] : '1';
        document.getElementById('logoswap').className += "class" + id;
      };
    })()
    

    I just want to take a moment here to
    encourage anyone who is doing this
    type of code to get used to Regular
    Expressions and learn them. They are
    far and away the most frequently used
    cross language part of my development
    arsenal.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns 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.