Results 1 to 2 of 2

Thread: Need little help with javascript.!

  1. #1
    dvp0008 Guest

    Need little help with javascript.!

    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( ){
    document.getElementById("menu").className="active";
    }

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

  2. #2
    Join Date
    Jul 2009
    Posts
    58
    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)
    {
    document.getElementById("menu").className="active";
    }

    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.

Similar Threads

  1. Replies: 0
    Last Post: 29 Jan 2010, 02:25 PM
  2. JavaScript Collector
    By JavaScriptBank in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 09 Jul 2009, 03:28 AM
  3. JavaScript library
    By JavaScriptBank in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 09 Jul 2009, 01:26 AM
  4. element specific javascript
    By gumbo in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 04 Dec 2005, 06:25 PM
  5. element specific javascript
    By gumbo in forum Web Design, HTML Reference and CSS
    Replies: 0
    Last Post: 04 Dec 2005, 06:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •