tomhoppe.com

Racing, Web Development, Photography, and Beer...Stuff that matters.

Wednesday, August 27, 2008

2 ways of Javascript menu or navigation highlighting

The following two ways are the way I do client side javascript menu highlighting. These work well if you have a site navigation or menu and you want to dynamically highlight the page that you are currently on. I use Method 1 when I have only a few items in the nav, as the JS for it is super easy, or I need to have relative URLs in my menu. Method 2 is better when there are a lot of items, as it relies only on the href and url and doesn't need any extra stuff in the HTML. It does need absolute URLs in the menu href's though in order to match correctly with the url.

Method 1 - This method relies on the fact that you will put id's on each of the a elements with the value of the file name of that file. The HTML shows what I mean. The JS then just looks at the file name of the URL and makes that ID active. Super easy. There is also an "else" statement at the end of the JS so if the file name is blank because you are on "http://yourdomain.com/" it goes ahead and highlights the index menu item.

<div id="topMenu">
    <ul>
        <li><a id="index" href="">Blog</a></li>
        <li><a id="about" href="about.html">About</a></li>
        <li><a id="race_car" href="race_car.html">Race Car</a></li>
        <li><a id="contact" href="contact.html">Contact</a></li>            
    </ul>
</div>
<script type="text/javascript" language="javascript">
var fileName=location.href.toLowerCase().substring( location.href.lastIndexOf("/")+1 ).split('.')[0];
if (document.getElementById(fileName)) {document.getElementById(fileName).className = "active";}
else {if (fileName == '') {document.getElementById('index').className = "active";}}
</script>

Method 2 - This method does not need any id's in the a elements. What it does it look through the href statements in your menu and compares them to the URL. This method works better for larger menus.

<div id="topMenu">
    <ul>
        <li><a href="http://www.tomhoppe.com/test/index.html">Blog</a></li>
        <li><a href="http://www.tomhoppe.com/test/about.html">About</a></li>
        <li><a href="http://www.tomhoppe.com/test/race_car.html">Race Car</a></li>
        <li><a href="http://www.tomhoppe.com/test/contact.html">Contact</a></li>            
    </ul>
</div>
<script type="text/javascript" language="javascript">
var theUrl = location.href.toLowerCase();
var navLinks = document.getElementById('topMenu').getElementsByTagName('a');

if (theUrl == 'http://www.tomhoppe.com/test/') { navLinks[0].className = 'active'; }
else { 
    for (var i=0; i<navLinks.length; i++) {
        var NavLinkUrl = navLinks[i].getAttribute('href').toLowerCase();
        if (NavLinkUrl == theUrl) {navLinks[i].className = 'active';}
    }
}
</script>

Labels: , ,

Does training on a video game actually help you in racing?

I've got a race at Barber Motorsports park in Birmingam, AL this weekend. As someone at work called it "LA" for "Lower Alabama". I have been there a few times for Grand Am races to spectate, but never driven the track. With the wear and tear implications,the fact thats its already a 3 race weekend (I'm running a SARRC Race Sat and Sunday, and Dan, the previous car owner, is driving in the Saturday ProIT), and the fact that I wanted to do this little experiment, I'm not running the test day on Friday. That means my first view/action on this track will be qualifying on Saturday morning for the SARRC race.

I'm usually pretty good at getting up to speed because of my autox experience, so I'm not TOO worried about this. Just in case though, I've spent quite a few hours behind the wheel of a couple of PC Racing Sims on the track. The two sims I've been playing are Race07 and rFactor. They are both fairly realistic and with my wheel/pedal setup, feel great. Also, compared to a few real videos of the track, I think they have the layout and corners fairly nailed. I've been driving a Mini Cooper in Race07, as its the closest thing to my Integra, and a VW Rabbit in rFactor.

I've probably got about 2 hours or so of video game track time now and plan to spend another hour or so over the next couple of days. We'll see how close the game is to real life and if things like brake points and landmarks are close enough so I can qualify near the pointy end of the field!

I also posted on the roadraceautox.com forum to see what others thought

Labels: ,

Wednesday, August 20, 2008

Work work work work .......

I've noticed that after working a TON, as in 80+ hour weeks for a few weeks, when that stops and I go back to "normal", I have a real hard time adjusting to "normal" life. I get home and its as if my brain is used to still running at 100mph and not being able to just "sit still". Real wierd feeling. Almost as if I'm lost just relaxing, and always feeling like its wierd that I'm not "doing something".

Or maybe I just need a vacation ...... :)

Labels: , ,

First foray in PHP

So I've been a windows guy personally. Its just always what the companies where I worked used, and what I personally used. The web forum that I run has been using an antiquated ASP forum software for a while though, and it was time to move on. After evaluating some of the options, looks like vBulletin using PHP was the best one.

I have to say that I'm very impressed with my experience so far. PHP is installed and has been running like a champ on our IIS 6.0 server. The vBulletin and mySQL installs both went super smooth and I had the new forum up and running and data transferred in no time. I also made some custom php pages for redirects and the like, as well as a few edits to vBulletin's pages.

I'm happy with the speed in which I was able to sort of "pick up" php. I was a little worried at first about being able to jump into it, but its like an "easy" ASP with a little bit of asp and javascript syntax all thrown into one. All in all, easy to learn and because of the community support, there is all kinds of mySQL and php info out there on google.

Labels: , ,

Monday, August 11, 2008

Regular Expression Cheat Sheet

I just wanted to give myself a reference for a regular expression cheat sheet. This is much easier then forgetting and having to google for what I need. Pretty much all you ever need to know to setup a regexp.

. Matches any single character (many applications exclude newlines, and exactly which characters are considered newlines is flavor, character encoding, and platform specific, but it is safe to assume that the line feed character is included). Within POSIX bracket expressions, the dot character matches a literal dot. For example, a.c matches "abc", etc., but [a.c] matches only "a", ".", or "c".
[ ] A bracket expression. Matches a single character that is contained within the brackets. For example, [abc] matches "a", "b", or "c". [a-z] specifies a range which matches any lowercase letter from "a" to "z". These forms can be mixed: [abcx-z] matches "a", "b", "c", "x", "y", and "z", as does [a-cx-z].

The - character is treated as a literal character if it is the last or the first character within the brackets, or if it is escaped with a backslash: [abc-], [-abc], or [a\-bc].

[^ ] Matches a single character that is not contained within the brackets. For example, [^abc] matches any character other than "a", "b", or "c". [^a-z] matches any single character that is not a lowercase letter from "a" to "z". As above, literal characters and ranges can be mixed.
^ Matches the starting position within the string. In line-based tools, it matches the starting position of any line.
$ Matches the ending position of the string or the position just before a string-ending newline. In line-based tools, it matches the ending position of any line.
\( \) Defines a marked subexpression. The string matched within the parentheses can be recalled later (see the next entry, \n). A marked subexpression is also called a block or capturing group.
\n Matches what the nth marked subexpression matched, where n is a digit from 1 to 9. This construct is theoretically irregular and was not adopted in the POSIX ERE syntax. Some tools allow referencing more than nine capturing groups.
* Matches the preceding element zero or more times. For example, ab*c matches "ac", "abc", "abbbc", etc. [xyz]* matches "", "x", "y", "z", "zx", "zyx", "xyzzy", and so on. \(ab\)* matches "", "ab", "abab", "ababab", and so on.
\{m,n\} Matches the preceding element at least m and not more than n times. For example, a\{3,5\} matches only "aaa", "aaaa", and "aaaaa". This is not found in a few, older instances of regular expressions.

Labels: , ,