Dynamic navigation jquery + php
/* ===========================================================================
*
* JQuery URL Parser
* Version 1.0
* Parses URLs and provides easy access to information within them.
*
* Author: Mark Perkins
* Author email: mark@allmarkedup.com
*
* For full documentation and more go to http://projects.allmarkedup.com/jquery_url_parser/
*
* ---------------------------------------------------------------------------
*
* CREDITS:
*
* Parser based on the Regex-based URI parser by Stephen Levithian.
* For more information (including a detailed explaination of the differences
* between the 'loose' and 'strict' pasing modes) visit http://blog.stevenlevithan.com/archives/parseuri
*
* ---------------------------------------------------------------------------
*
* LICENCE:
*
* Released under a MIT Licence. See licence.txt that should have been supplied with this file,
* or visit http://projects.allmarkedup.com/jquery_url_parser/licence.txt
*
* ---------------------------------------------------------------------------
*
* EXAMPLES OF USE:
*
* Get the domain name (host) from the current page URL
* jQuery.url.attr("host")
*
* Get the query string value for 'item' for the current page
* jQuery.url.param("item") // null if it doesn't exist
*
* Get the second segment of the URI of the current page
* jQuery.url.segment(2) // null if it doesn't exist
*
* Get the protocol of a manually passed in URL
* jQuery.url.setUrl("http://allmarkedup.com/").attr("protocol") // returns 'http'
*
*/
jQuery.url = function()
{
var segments = {};
var parsed = {};
/**
* Options object. Only the URI and strictMode values can be changed via the setters below.
*/
var options = {
url : window.location, // default URI is the page in which the script is running
strictMode: false, // 'loose' parsing by default
key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"], // keys available to query
q: {
name: "queryKey",
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
},
parser: {
strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, // more intuitive, fails on relative paths and deviates from specs
loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ //less intuitive, more accurate to the specs
}
};
var parseUri = function()
{
str = decodeURI( options.url );
var m = options.parser[ options.strictMode ? "strict" : "loose" ].exec( str );
var uri = {};
var i = 14;
while ( i-- ) {
uri[ options.key[i] ] = m[i] || "";
}
uri[ options.q.name ] = {};
uri[ options.key[12] ].replace( options.q.parser, function ( $0, $1, $2 ) {
if ($1) {
uri[options.q.name][$1] = $2;
}
});
return uri;
};
var key = function( key )
{
if ( ! parsed.length )
{
setUp(); // if the URI has not been parsed yet then do this first...
}
if ( key == "base" )
{
if ( parsed.port !== null && parsed.port !== "" )
{
return parsed.protocol+"://"+parsed.host+":"+parsed.port+"/";
}
else
{
return parsed.protocol+"://"+parsed.host+"/";
}
}
return ( parsed[key] === "" ) ? null : parsed[key];
};
var param = function( item )
{
if ( ! parsed.length )
{
setUp(); // if the URI has not been parsed yet then do this first...
}
return ( parsed.queryKey[item] === null ) ? null : parsed.queryKey[item];
};
var setUp = function()
{
parsed = parseUri();
getSegments();
};
var getSegments = function()
{
var p = parsed.path;
segments = []; // clear out segments array
segments = parsed.path.length == 1 ? {} : ( p.charAt( p.length - 1 ) == "/" ? p.substring( 1, p.length - 1 ) : path = p.substring( 1 ) ).split("/");
};
return {
setMode : function( mode )
{
strictMode = mode == "strict" ? true : false;
return this;
},
setUrl : function( newUri )
{
options.url = newUri === undefined ? window.location : newUri;
setUp();
return this;
},
segment : function( pos )
{
if ( ! parsed.length )
{
setUp(); // if the URI has not been parsed yet then do this first...
}
if ( pos === undefined )
{
return segments.length;
}
return ( segments[pos] === "" || segments[pos] === undefined ) ? null : segments[pos];
},
attr : key, // provides public access to private 'key' function - see above
param : param // provides public access to private 'param' function - see above
};
}();
$(function(){
//Navegacion
$page = $.url.param("p");
if(typeof $page === 'undefined') {
$page = 'inicio';
}
$('nav ul li a').each(function(){
var $href = $(this).attr('href');
if ($href.toLowerCase().indexOf($page) >= 0){
$(this).parent().addClass('activo');
} else {
$(this).parent().removeClass('activo');
}
if($page == 'inicio'){
$(this).closest('ul').find('li:eq(0)').addClass('activo');
}
});
});
<ul>
<li><a href="<?php echo $url;?>">INICIO</a></li>
<li><a href="<?php echo $url;?>?p=page">NUESTRA EMPRESA</a></li>
</ul>
<?php
if(isset($_GET['p']) && $_GET['p'] != ''){
$seccion = $_GET['p'];
}
else{
$seccion = 'inicio';
}
sectionWeb($seccion);
?>
<?php
// detect a localhost of a domain up
$weblocal = 'folderweb/'; //localhost folder
$server = $_SERVER['SERVER_NAME'];
if(strstr($server, '192.168.1.10') || strstr($server, 'localhost')){
$url = "http://" . $_SERVER['SERVER_NAME'] . "/" . $weblocal;
} else {
$url = "http://" . $_SERVER['SERVER_NAME'] . "/";
}
//must exist the file section with the same name in the navigation
function sectionWeb($seccion){
global $url;
if(file_exists('vistas/' . $seccion . '.php')){
if(isset($_GET['s']) && $_GET['s'] != ''){
$subpage = $_GET['s'];
if(file_exists('vistas/' . $subpage . '.php'))
include('vistas/' . $subpage . '.php');
else
include('vistas/' . $seccion . '.php');
} else {
include('vistas/' . $seccion . '.php');
}
} else{
include('vistas/inicio.php');
}
}
?>