Thursday, 14 May 2015

How to add class based on active URL in PHP and in jQuery


//Past into Header
<?php
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$page_class =explode('/',$actual_link);
?>




<body class="<?=end($page_class);?>">











================= jQuery ==============
<script>

$(function() {
  var loc = window.location.pathname; // returns the full URL
var split_loc = loc.split('/');
active_loc = split_loc[split_loc.length-1];
$('body').addClass(active_loc + "-page");
});

</script>

No comments:

Post a Comment

Alternative content