javascript - How to change the content of a file which is included in different scripts? -
i making website have make page active attributes on different lines different pages. put whole common script inside file header , include in every script. how can change content of line when including on different pages?
the header follows:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>bootstrap 101 template</title> <!-- bootstrap --> <link href="css/bootstrap.min.css" rel="stylesheet"> <link href="islamic.css" rel="stylesheet"> <link href='http://fonts.googleapis.com/css?family=lato:400,300italic' rel='stylesheet' type='text/css'> <!-- html5 shim , respond.js ie8 support of html5 elements , media queries --> <!-- warning: respond.js doesn't work if view page via file:// --> <!--[if lt ie 9]> <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <![endif]--> <style> html,body { height: 1500px; } .grey { background-color: #ccc; padding: 20px; } </style> </head> <body> <!-- going make page navigation purpose --> <nav class="navbar navbar-inverse navbar-fixed-top" role = "navigation"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="icon-bar"> </span> <span class="icon-bar"> </span> <span class="icon-bar"> </span> </button> <a class="navbar-brand" href="index.php">ilm</a> </div> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li ><a href="index.php">home</a></li> <li ><a href="why.php">discuss</a></li> <li ><a href="aboutus.php">about us</a></li> <li ><a href="contacts.php">contact</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown"> social <b class="caret"> </b></a> <ul class="dropdown-menu"> <li> <a href="http://www.facebook.com">facebook</a></li> <li> <a href="#">twitter</a></li> <li> <a href="#">linked in</a></li> <li> <a href="#">you tube</a></li> </ul> </li> </ul> </div> </nav> i want change line
<li ><a href="index.php" >home</a></li> to
<li class="active"><a href="index.php">home</a></li> for home page. want change line
<li ><a href="aboutus.php" >about us</a></li> to
<li class="active"><a href="aboutus.php">home</a></li> for page. possible in php?
you can try somethign this
http://jsfiddle.net/habo/arjw2q6b/
place code @ end of </body> tag. guessing page urls have anchor tags hrefs. 1 each page load after full document id read script check if url contains "index.php" or other hrefs , if call function resetactivetab function remove active class list items (li) , add class=active current page
$(function(){ if(window.location.href.contains("index.php")){ resetactivetab("index.php"); } else if(window.location.href.contains("why.php")){ resetactivetab("why.php"); } else if(window.location.href.contains("aboutus.php")){ resetactivetab("aboutus.php"); } } function resetactivetab(myselector){ $(".navbar-nav li").each(function( key, value ) { //alert($(value).attr("class")); $(value).removeclass("active"); if( $(value).find("a").attr("href") == myselector){ $(value).addclass("active"); } }); }
Comments
Post a Comment