View Full Version : Need little help with javascript.!

23 Aug 2010, 02:16 PM
Hi all, I am trying to change a class of a div depending on in whichever page it is envoked.
like menu items are in header.php , which i am including( include php) in lot of pages, but depending on page i want to change color of a menu item.

I am confused what should be the If statement should be,
like ,

If( ){

which in simple english might say :[ " if current page is some.php than change the class name of "menu" id to "active"]

31 Aug 2010, 06:24 AM
I wouldn't do it with javascript but with PHP. Even though you can do it with javascript it is not as neat.

where some.php can be the name of any page or just any string

Javascript version:

if(window.location.pathname.indexOf("some.php") > 0)

window.location.pathname returns a string with the name of the current page preceded by a \
indexOf detects if some.php can be found within that string and returns its position

PHP version:

if(strpos($_SERVER['PHP_SELF'], "some.php") > 0)
echo "<div class='active'>$your webpage</div>"; //depending on the structure of your site

works the same way as the javascript statement above, except this time the function is strpos
There are certainly better ways to do in PHP, but it depends on how your website is structured.