Set active links by current URL for Open-Realty®

DEMO:

I decided to share this VERY SIMPLE code of JavaScript+PHP+CSS I created. Always wanted to have a menu with "active" links, dependent on URL value for Open-Realty® (download example file HERE). So, I created the following code:

<?php

echo '<script type="text/javascript">
$(document).ready(function(){';
if(intval($_GET['PageID']) > 0) {
	$from_url = intval($_GET['PageID']);
	} else {
		if(isset($_GET['action'])) {
		$from_url = $_GET['action'];
		} else {
		$from_url = 'index';
		}
	}
echo '
	$(\'.curr_' . $from_url . '\').addClass(\'active\');
	});
</script>';

?>
		

The PHP code above is parsing to the browser a JavaScript (jQuery dependent) code. First, the PHP code check if there is an integer "PageID" present at the URL (and get it) if not, the PHP code get the "action" value (a text value). The result is assigned to a PHP variable named "$from_url" and after that jQuery add a class name "active" for the HTML element with a class "curr_" + "$from_url" value.

The result printed at your browser will be simply:
("###" will be the value grabbed from the URL)

<script type="text/javascript">
$(document).ready(function(){
	$('.curr_###').addClass('active');
	});
</script>
		

The above DEMO have this HTML code:

<ul>
	<li class="curr_index"><a href="./">LINK 01</a></li>
	<li class="curr_2"><a href="index.php?action=page_display&PageID=2#test">LINK 02</a></li>
	<li class="curr_searchresults"><a href="index.php?action=searchresults">LINK 03</a></li>
	<li class="curr_searchpage"><a href="search.html">LINK 04</a></li>
</ul>
		

Notice "LINK 01" have no "action" or "PageID" set. Notice "LINK 02" have an action set as "page_display" but also has a "PageID" (and a "hash"). Notice "LINK 03" have only an action set. Notice "LINK 04" have a SEF link.

The CSS code... can be whatever you wish.

For better understanding, for example:
- "LINK 02" have a class name "curr_2"... the integer "2" will only be grabbed from the URL if the current page is "index.php?action=page_display&PageID=2"
- "LINK 03" have a class name "curr_searchresults"... the value "searchresults" will only be grabbed from the URL if the current page is "index.php?action=searchresults"

Of course, my code can be customized for different results and for different softwares.
It was tested with Open-Realty v.3.2.4 and ALL major browsers IE6/7/8/9, Firefox, Chrome and Safari.
I hope it will be useful for you! Please keep my copyright message (at the example for download above).

Enjoy it! :)
Eduardo Marques (a.k.a. "ebmarques")
Aug,01/2012