View Full Version : How do I display or hide a Javascript menu based on the current url in a php page?

07-15-2009, 10:04 AM
How do I display or hide a Javascript menu based on the current url in a php page?

I want to specify when a java menu appears on a page based on the current url - does anyone have a script for the vars and conditional statements to make this happen - it would be something like this in natural language:

if currenturl = http://jemmakidd.com/categories.php
then displaymenu
else if currenturl = http://jemmakidd.com/categories.php?cat=14 (or 15,16 etc)
then DONOTdisplaymenu

What is needed is that, if you look at this page there is a java menu under the first category:


I WANT the menu to appear on that page so that is working, however if I look at one of the store categories, like on this page, I do NOT want the menu to appear - it may be that this can be done without using a java current url type function - thats why I'm asking...


So to summarise:

http://jemmakidd.com/categories.php (If this url is used DO display menu - if any other url DONT display menu)

Thanks again...any ideas?

07-15-2009, 10:14 AM
This is the menu code I want to fire IF the url is http://jemmakidd.com/categories.php followed by the html to display the menu...


<script type="text/javascript">

//SuckerTree Vertical Menu 1.1 (Nov 8th, 06)
//By Dynamic Drive: http://www.dynamicdrive.com/style/

var menuids=["suckertree1","suckertree2"] //Enter id(s) of SuckerTree UL menus, separated by commas

function buildsubmenus(){
for (var i=0; i<menuids.length; i++){
var ultags=document.getElementById(menuids[i]).getElementsByTagName("ul")
for (var t=0; t<ultags.length; t++){
if (ultags[t].parentNode.parentNode.id==menuids[i]) //if this is a first level submenu
ultags[t].style.left=ultags[t].parentNode.offsetWidth+"px" //dynamically position first level submenus to be width of main menu item
else //else if this is a sub level submenu (ul)
ultags[t].style.left=ultags[t-1].getElementsByTagName("a")[0].offsetWidth+"px" //position menu to the right of menu item that activated it
for (var t=ultags.length-1; t>-1; t--){ //loop through all sub menus again, and use "display:none" to hide menus (to prevent possible page scrollbars

if (window.addEventListener)
window.addEventListener("load", buildsubmenus, false)
else if (window.attachEvent)
window.attachEvent("onload", buildsubmenus)



<td width="265" valign="top"><div class="suckerdiv">

<div align="left">
<ul id="suckertree1">
<li><a href="#">FACE</a>
<li><a href="#">PRIMER</a></li>
<li><a href="#">FOUNDATION</a></li>
<li><a href="#">POWDER</a></li>
<li><a href="#">CONCEALER</a></li>
<li><a href="#">BLUSH</a></li>
<li><a href="#">LUMINISORS</a></li>
<li><a href="#">EYES</a>
<li><a href="#">PRIMER</a></li>
<li><a href="#">SHADOW</a></li>
<li><a href="#">MASCARA</a></li>
<li><a href="#">BROW</a></li>
<li><a href="#">LASH</a></li>
<li><a href="#">LINER</a></li>
<li><a href="#">GLOSS</a></li>
<li><a href="#">LIPS</a>
<li><a href="#">PRIMER</a></li>
<li><a href="#">LIP CARE</a></li>
<li><a href="#">LIPSTICK</a></li>
<li><a href="#">LIP GLOSS</a></li>
<li><a href="#">LIP PENCIL</a></li>
<li><a href="#">TOOLS</a>
<li><a href="#">EYE</a></li>
<li><a href="#">FACE</a></li>
<li><a href="#">LIP</a></li>
<li><a href="#">BAGS/CASES</a></li>
<li><a href="#">LIP PENCIL</a></li>
<li><a href="#">COLLECTIONS</a></li>
<li><a href="#">MULITI-PURPOSE</a></li>

Philip M
07-15-2009, 10:42 AM
<div id = "theMenu" style="display:block" >The menu in here</div>

<script type="text/javascript">
var nowUrl = location.href;
if (nowUrl != "http://jemmakidd.com/categories.php") {


This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.

Following protracted negotiations we have come to an arrangement with the banks under which we have agreed not to supply goods to our customers on credit. For their part the banks have agreed not to sell fishing equipment.

07-15-2009, 11:06 AM
Thanks SOOO much Philip - that code worked perfectly and has saved me a real headache!

Just so I know in future - where SHOULD I be posting on this language...?

A :)

Philip M
07-15-2009, 11:17 AM
Javascript = http://www.codingforums.com/forumdisplay.php?f=2

Java = http://www.codingforums.com/forumdisplay.php?f=54

As I say, these are entirely different languages. Like Austrian and Australian :D

07-15-2009, 12:54 PM
haha - cheers... :thumbsup: