ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Set CSS based on #anchor in URL using Javascript (I assume)

September 15, 2010 10:13pm

Subscribe [1]
  • #1 / Sep 15, 2010 10:13pm

    johnboat

    61 posts

    I am trying to highlight a specific word when a page loads based on the #anchor in the url. On one page a person clicks a product link that takes them to a specific place on another page (hence the anchor tag). I then want that item highlighted.

    I first thought about segment variables but the #anchor portion isn’t really part of the url so that doesn’t work. I then found something close using Javascript here.

    Here is the original code from that page:

    [removed]
    
        function showHash() {
            var currentUrl = "" + document.location;
            var hash = "";
            var parts = currentUrl.split("#");
            if (parts.length > 1) {
                hash = parts[1];
            }
            alert("the current hash is: " + hash);
        }
    
    [removed]
    
    <input type="button" value="Show Hash">

    I tested that and it works, so I know I can get the #anchor name with this script, what I can’t figure out is how to translate that to highlighting the element I want (say the url has #car in it, I would want to highlight the table cell with the id of “car”.

    I tried replacing this part of the above code:

    alert("the current hash is: " + hash);

    with this:

    document.getElementById(hash).style.backgroundColor = "yellow";

    But no joy. I’m not a javascript guy so I know I am doing something really dumb, but any help or suggestions would be greatly appreciated.

    Thanks,
    John

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases