Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Assigning CSS Class By URL Pathname</h2> <blockquote> <p>A <a href="http://jsfiddle.net/B347f/" rel="nofollow">jsfiddle</a> has been setup for this solution.</p> </blockquote> <p>Here is a case for using numeric expressions if they are available. This does not apply to the above question.</p> <pre><code>$(function() { var rgx = /item(\d+)$/, url = location.pathname, id = (rgx.test(url)) ? url.match(rgx)[1] : '1'; $("#logoswap").addClass("class" + id); }); </code></pre> <p><strong>UPDATE:</strong></p> <p>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.</p> <pre><code>$(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 &gt; -1) ? id : 0]; $("#logoswap").addClass(className); }); </code></pre> <p>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 <code>1</code>. You will need the images to be accessible. You need to define the styles for each possibility.</p> <hr> <h2>CSS Setup</h2> <pre><code>#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); } ... </code></pre> <hr> <h2>Without jQuery</h2> <p>if you do not have jQuery in your code you may need to use <code>window.onload</code>.</p> <pre><code>(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; }; })() </code></pre> <blockquote> <p>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.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload