I am creating menu, and if the specific page is current , menu item assign different color.
Simple situation.
So i do :
<?php $page = $_SERVER['SCRIPT_NAME'];?>
<?php if ($page == "index.php"){ echo "class='active'";} ?> >
Home
That works for every page, except one.
I tried some other options suggested on stackoverflow
How to get current PHP page name , like :
echo basename($_SERVER['PHP_SELF']); returns file_name.php
and
if(basename(__FILE__) == 'file_name.php') {
//Hide
} else {
//show
}
Still does not work.
$pagename = basename($_SERVER['PHP_SELF']);
if($pagename=='index.php'){
echo "class='active'";
}
According to the data you supplied I suggest to use client-side solution instead of server-side solution. I mean that you use javascript.
This is may be done as follows:
<ul id="menu">
<li>Home</li>
<li>About</li>
<li>Any Other</li>
</ul>
<script>
function setCurrentNavLink(id){
nav = document.getElementById(id);
links = nav.getElementsByTagName('a');
for (i = 0; i < links.length; i++){
if (links[i].href == location.href){
links[i].className = 'current';
break;
}
}
}
setCurrentNavLink('menu');
</script>
Check this demo: http://jsbin.com/tomid/1
Sorted. The problem was in JQuery , thats operates Tabs on the current page.
$(document).ready(function(){
// When a link is clicked
$("a.tab").click(function () {
// switch all tabs off
$("ul.tabs li a.active ").removeClass("active");
//$(".active ").removeClass("active");
// switch this tab on
$(this).addClass("active");
//$(".collapse navbar-collapse").addClass("active");
// slide all content up
$(".content").slideUp();
// slide this content up
var content_show = $(this).attr("title");
$("#"+content_show).slideDown();
});
});
The mistake was in 3rd section, where Class .active was removed from Every object. Including header. Therefore header did not appear as active.
Instead of that, class .active had to be removed only from Tabs tab.
Related
I'm now stuck with this problem below, and I hope you can help me out. I would really appreciate it.
1st -- I created the Wordpress Menu (got it all worked).
2nd -- I implemented the Bootstrap classes (e.g nav navbar-nav, dropdown, dropdown-menu etc..) on the html tags of the Wordpress Menu. (got it and made it work somehow)
3rd -- To make the dropdowns work I used (preg_place) to replace the default (submenu) to (dropdown-menu) then to make the function work I used the (preg-replace) again to INSERT a class and data-toggle to the Anchor Links.
Now the problem is, the anchor links doesn't seem to function right at all.
check it out here.
(use the navs on top)
HEres the code on the function.php
function new_submenu_class($menu) {
$menu = preg_replace('/ class="sub-menu"/','/ class="dropdown-menu" /',$menu);
return $menu;
}
add_filter('wp_nav_menu','new_submenu_class');
function add_menuclass($ulclass) {
$ulclass = preg_replace('/<a/', '<a class="dropdown-toggle" data-toggle="dropdown" role="button"', $ulclass);
return $ulclass;
}
add_filter('wp_nav_menu','add_menuclass');
Thanks much guys!
And btw.. i used a jQuery for the 3rd level submenu. which is:
<script type="text/javascript">
$(document).ready(function() {
$('.navbar a.dropdown-toggle').on('click', function(e) {
var $el = $(this);
var $parent = $(this).offsetParent(".dropdown-menu");
$(this).parent("li").toggleClass('open');
if(!$parent.parent().hasClass('nav')) {
$el.next().css({"top": $el[0].offsetTop, "left": $parent.outerWidth() - 4});
}
$('.nav li.open').not($(this).parents("li")).removeClass("open");
return false;
});
});
</script>
In the function, I would try to change this line:
$('.navbar a.dropdown-toggle').on('click', function(e) {
For this one:
$('.navbar').on('click', 'a.dropdown-toggle', function(e) {
Since a.dropdown-toggle has been happended...
Maybe it was not present on "document ready", so the "delagation" syntax could help.
So, im building a website and i want to give a "active" class to my menu depending on the page the user access. I'm new to Jquery, so i don't know if this is the best way to do it, or if there is a better code to achieve what i want.
Currently my main structure is:
Header
Navbar where is the menu i'm giving the active class
Content called with the php code
Footer
The only thing i need to change when loading another page, is the content it doesn't really matter if it's php or ajax. Actually, i know ajax can do it more dynamic, but i just don't know how to do it yet.
So this is the code i made to give a active class to my li a element correspondent to the webpage:
php to load page:
<?php
if(!empty($_GET['p'])){
if (file_exists ($_GET['p'].".php")){
include($_GET['p'].".php");
} else include ("erro.php");
} else include("content/home.php");
?>
Jquery to give it a class:
$(document).ready(function() {
var home = $('a[href$="home"]'),
servicos = $('a[href$="servicos"]'),
advogados = $('a[href$="advogados"]'),
escritorio = $('a[href$="escritorio"]'),
noticias = $('a[href$="noticias"]'),
contato = $('a[href$="contato"]');
$(function() {
var loc = window.location.href;
if(/home/.test(loc)) {
$('.navbar li').find(home).addClass('active');
} else if(/servicos/.test(loc)) {
$('.navbar li').find(servicos).addClass('active');
} else if(/advogados/.test(loc)) {
$('.navbar li').find(advogados).addClass('active');
} else if(/escritorio/.test(loc)) {
$('.navbar li').find(escritorio).addClass('active');
} else if(/noticias/.test(loc)) {
$('.navbar li').find(noticias).addClass('active');
} else if(/contato/.test(loc)) {
$('.navbar li').find(contato).addClass('active');
};
});
});
Is there another way or a best/easy way to do it?
Since you are using jQuery, take a look at jQuery's load function. Using load you can specify an html element(usually div) in which you will load pages, like this:
$("#myDiv").load("/my/page/folder/page.html");
To handle the menu you will need this piece of code:
// Select all menu links and attack event handler on them
$(".navbar li a").on("click", function(event){
// This stop the link from loading into the browser tab(we want it to load in a div)
event.preventDefault();
// Remove any previous highlight
$(".active").removeClass("active");
// Add highlight to the menu we clicked
$(this).addClass("active");
// Load the link into "myDiv"
$("#myDiv").load(this.href);
});
You may need to tweak around with this code, i haven't tested it, but this is how it works in general.
Note: in the pages you load, using jQuery's load function, you need only the content and not the whole HTML structure.
If you want to load only part of the page you can do so like this:
$("#myDiv").load("/my/page/folder/page.html #container");
Your method of page's inclusion is extremely dangerous as I can basically access any php file on your server.
I would recommend to use an array that contains all the allowed pages, like:
$pages = array('home', 'about', 'contact');
$current_page = '';
//Default page
if(!isset($_GET['p']) || empty($_GET['p'])){
$current_page = 'home';
}
Now, when you print the menu, you can do something like this:
foreach($pages as $page){
echo '<li><a href=""';
if($page == $current_page) echo ' class="active"';
echo '></a></li>';
}
You can expand your array of pages to contain also title, meta tags, etc.
For instance:
$pages = array(
'home' => array('title' => 'Home', 'meta_description' => 'Bla Bla Bla'),
'about' => array('title' => 'About Us', 'meta_description' => 'Bla Bla Bla')
);
I am making a website in HTML, CSS and PHP. I have a navigation on the top of the page, and when you hover your mouse over the buttons, they light up.
Now, when you click a button and go to that particular page, I would like the button for that page to be lit up, indicating that you are on that page. How would I go about doing this? Defining a variable on each page and then checking for it in the menu is not possible, as I have a forum on the site too, and that would require me to define a variable on each page.
EDIT
I managed to solve my problem. As it turns out, I could just define the pages, and for the forum I could do the same in the settings file that the forum used.
In my navigation, I just check what the current page is:
<li id="news"><a <? if(PAGE == "INDEX") print 'class="active"'; ?> href="/">News</a></li>
Add a class (or ID) to the body tag, like so:
<body class="Page1">...</body>
Then, in your CSS you can say something like:
.Page1 menu li, menu li:hover {...}
I'm not sure how to check for the current page in PHP - though, that would be ideal.
If you want to rely on JavaScript, though, I have used this function in the past successfully:
function highlightPage(id){
//make sure DOM methods are understood
if(!document.getElementsByTagName) return false;
if(!document.getElementById) return false;
if(!document.getElementById(id)) return false;
var nav = document.getElementById(id);
var links = nav.getElementsByTagName('a');
for (var i=0; i < links.length; i++){
var linkurl = links[i].getAttribute('href');
var currenturl = window.location.href;
//indexOf will return -1 on non-matches, so we're checking for a positive match
if (currenturl.indexOf(linkurl) != -1) {
links[i].className = "here";
var linktext = links[i].lastChild.nodeValue.toLowerCase();
document.body.setAttribute("id",linktext);
}
}
}
And to load the function at the load of the page:
function addLoadEvent(func){
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function(){
oldonload();
func();
}
}
}
addLoadEvent(function(){
highlightPage('navigation');
});
This assumes that your navigation has the ID of "navigation", but you can change it to whatever you want. The function simply attaches a class of "here" to the current navigation item.
This script comes from Jeremy Keith's "DOM Scripting".
how to get all anchor tag of page and add one class to mailto (eg. mailto:example#example.com) and another to web link (eg. http://example.com) ?
output expected :
<a href="mailto:example#example.com" class="class1">
<a href="http://example.com" class="class2">
$('a[href^="mailto:"]').addClass('class1');
$('a:not([href^="mailto:"])').addClass('class2');
Example: http://jsfiddle.net/HackedByChinese/3t2MQ/2/
Update Use :not() instead of [href^="http"].
You can use this JQuery to get a collection of anchor elements:
var elements = $("a");
from there you can loop through each and check the href value:
elements.each(function(){
var a = $(this);
if(a.attr("href").indexOf("mailto:") == 0){
a.addClass("MailToClass");
}
else{
a.addClass("HttpClass");
}
});
$(document).ready(function() {
$.each($('a'), function(k, v) {
var href = $(v).attr('href');
if (href.indexOf('mailto') != -1)
$(v).addClass('class1');
else
$(v).addClass('class2');
});
});
try this:
$('a[href^="mailto').each(function(){
$(this).addClass('sth')
})
Use the jQuery .each() function to loop through all the anchor elements and check the href so you know which class to add.
Example
$('a').each(function(){
if($(this).attr('href') == 'mailto:example#example.com'){
$(this).addClass('class1');
}
else if($(this).attr('href') == 'http://example.com'){
$(this).addClass('class2');
}
});
$("a").each(function(){
if($(this).attr('href').indexOf('mailto:') == 0) {//mailto link
$(this).addClass('class1')
}
else {
$(this).addClass('class2')
}
});
You could use something like this
$('a[href^="mailto:"]').addClass('class1'); //This line adds a class to any href starting with mailto
$('a[href^="http"]').addClass('class2'); //This line adds a class to any link starting with http
to find tag in same document do like
$('a[href^="mailto:"]').addClass('class1');
$('a:not([href^="http:"])').addClass('class2');
to find in iframe do like
jQuery("a[href^='mailto:']",jQuery("#myiframe")[0].contentDocument).addClass('class1');
jQuery("a:not([href^='http:'])",jQuery("#myiframe")[0].contentDocument).addClass('class2');
i want to change a div content when my page embed into another page
for exemple:
if the visitor access directly to the page:
<div id="msg"> this is my page </div>
if my page embed into a frame
<div id="msg"> my page into anthor page :)</div>
thanks for advance.
You can easily check if page is in iframe using this trick:
var isInIFrame = (self != top);
So, you check it on page-loading and get something like this:
$(document).ready(function() {
var isInIFrame = (self != top);
if (isInIFrame)
$("#msg").text("my page into anthor page :)");
});
And default value "this is my page" should be in markup file.
You could do it with javascript (client side only). From this link;
http://billhiggins.us/blog/2009/04/09/detecting-that-youre-in-an-iframe/
You could do the following with javascript;
<script type = "text/javascript">
if(window !== top) {
document.getElementById("msg").innerHtml("My page into another page")
}
</script>
Not tested, and from reading that article, issues to consider, but hopefully this gets you in the right direction.
You can try:
if (window != top) {
document.write('<div id="msg"> my page into anthor page :)</div>');
} else {
document.write('<div id="msg"> this is my page </div>');
}
You might be able to accomplish this with JavaScript. You can detect whether your window object is the top-level browser window:
if (window === top){
alert("I'm the top.");
} else {
alert("I'm embedded.");
}