Free Web Hosting Forum
Go Back   Free Web Hosting Forum > Website Building > Scripts and Code Snippets
Reload this Page How to get the current page selecte - using only js
Reply
 
Thread Tools Display Modes
(#1 (permalink))
Old
Junior Member
Iguess is on a distinguished road
 
Posts: 21
Join Date: Aug 2011
Default How to get the current page selecte - using only js - 08-29-2011, 06:13 PM

Hi,

If you are building small websites without any frameworks... here is a small code that will add to the anchor in the menu, the class "selected" - and then you do whatever you want with it in CSS.



PHP Code:
// first need to include the JQuery URL Parser plugin - https://github.com/allmarkedup/jQuery-URL-Parser

$(document).ready(function() {
    
selectCurrentMenuLink();    
});

function 
selectCurrentMenuLink () {

    
//$.url(); is a jquery plugin
    
var url = $.url();    
    $(
'#Menu a').each(function() {
        
//if the href of a particular a == the url of the page - make page selected
        
if ($(this).attr('href') == url.attr('file')) {
            $(
this).addClass("selected");
        } else {
//remove the selected class
            
$(this).removeClass("selected");
        }
    });

the html code can look something like:

PHP Code:
<div id="Menu" class="box1" >
                <
ul>
                <
li><a href="index.php"><span>Home</span></a></li>
                <
li><a href="websites.php"><span>Websites</span></a></li>
                <
li><a href="portfolio.php"><span>Portfolio</span></a></li>
                <
li><a href="about.php"><span>About me CV</span></a></li>
                <
li><a href="contact.php"><span>Contact</span></a></li>
                </
ul>
            </
div

What happens is that you loop trough all the anchors in one div (menu) and if one of them == the link of the current page, the class selected is added to the anchor...

Cheers!

PS
I have an issue with it on my website and currently not working - but is only happening on this server - on my server is working fine... please let me know if is working for you.


if you need a website... checkout my website - http : // alex-toader . com
Reply With Quote
Sponsored Links
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.5.2
vBulletin Skin developed by: vBStyles.com