HTML and CSS Reference
In-Depth Information
that can do everything server-side includes can do and a lot more. Many web
hosting companies enable PHP by default. Check the support pages of your
web hosting company, or run a quick test, before deciding to use server-side
includes or some other server-side technology.
Oten a section of included code is exactly the same on a number of web-
site pages except for just one tiny detail. An example is a main menu with a
requirement to highlight the link corresponding to the current page. here is
no reason to despair. You can use JavaScript or jQuery ater the page loads to
ix things.
Let's say you have a menu with three items deined in an unordered list:
<ul id="nav">
<li><a href="index.shtml"> Home </a></li>
<li><a href="about.shtml"> About </a></li>
<li><a href="contact.shtml"> Contact </a></li>
</ul>
his navigation list is included in every page on your site. You need a way to
compare the location of the current page to the values of the href attributes
in the list of links. he following two statements create a JavaScript variable,
this_page , containing the ilename of the current page:
last_slash = location.pathname.lastIndexOf('/');
this_page = location.pathname.substr(++last_slash);
he irst statement inds the location of the last slash in the current page's
URL. he second statement extracts from the URL the substring following
that last slash. 3 Now you need a jQuery expression that inds the appropriate
link in the menu and does something with it. Here is such an expression:
$(document).ready(function () {
var s = '#nav a[href="' + this_ page + '"]';
$(s).addClass('thispage');
});
Briely, this expression calls an anonymous function when the document is
ready and the DOM is fully deined. he function selects the anchor element
3. To extract the ilename without the slash, one must be added to the index of the last slash. his is done
with the increment operator ( ++ ), because the plus operator ( + ) in JavaScript means string concatena-
tion. In other words, 2 + 2 = 22 in JavaScript, not 4. his annoying dual use of the plus symbol is the
source of many JavaScript bugs.
Search WWH ::




Custom Search