473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

loading menu using javascript

54
Hello,
I want to use Images as menu and menu items using javascript.The aim is that i have images with the menu items. when i click the image on that perticular mmenu item i want an image to be loaded there with that menu item highlighted.

Suppose the menu has
Home,Products,Contact us
The image has these items.
When the user clicks Home , i want the Image with Home highlighted to be loaded. when the user clicks Contact Us, The image with Contact Us highlighted to be loade.
Since this is a single image how can i detect what Menu Item or option is clicked by the user?
Please let me know asap
Dec 20 '07 #1
5 1534
Hello,
I want to use Images as menu and menu items using javascript.The aim is that i have images with the menu items. when i click the image on that perticular mmenu item i want an image to be loaded there with that menu item highlighted.

Suppose the menu has
Home,Products,Contact us
The image has these items.
When the user clicks Home , i want the Image with Home highlighted to be loaded. when the user clicks Contact Us, The image with Contact Us highlighted to be loade.
Since this is a single image how can i detect what Menu Item or option is clicked by the user?
Please let me know asap
Hi,
Expand|Select|Wrap|Line Numbers
  1. test_image = new Image();
  2. function getLeaf(url)
  3. {
  4.     return url.substring(url.lastIndexOf("/")+1);
  5. }
  6. function highlightCurrentMenuItem()
  7. {
  8.     var currentLocation = getLeaf(document.location.href);
  9.     var menu = document.getElementById("menu");
  10.     links = menu.getElementsByTagName("a");
  11.     for (i=0; i<links.length; i++)
  12.     {
  13.         var currentHref = links[i].getAttribute("href");
  14.         var currentLeafName = getLeaf(currentHref);
  15.         if (currentLeafName==currentLocation)
  16.         {
  17.             document.test_image.src = "images/globe.gif";
  18.         }
  19.     }
  20. }
  21.  
In every page use
[HTML]<body onload="javascript:highlightCurrentMenuItem();">[/HTML]
In other pages do like this;

In home page
[HTML]<li><a href="index.html" title="">
<img name="test_image" id="test_image" src="images/color_spacer.gif" align="top" />Home</a></li>
<li><a href="" title="">About Us</a></li>
<li><a href="" title="">Services</a></li>
<li><a href="" title="">Portofolio</a></li>
<li><a href="contact.html" title="">Contact</a></li>[/HTML]

In contact_us page
[HTML]<li><a href="index.html" title="">Home</a></li>
<li><a href="" title="">About Us</a></li>
<li><a href="" title="">Services</a></li>
<li><a href="" title="">Portofolio</a></li>
<li><a href="contact.html" title="">
<img name="test_image" id="test_image" src="images/color_spacer.gif" align="top" />Contact</a></li>[/HTML]

SO ON.....

Happy Programming!
:)
Dec 20 '07 #2
Hi,
Forgot to say: color-spacer.gif is a blank image wich is replaced by globe.gif.
You can change the image as per your requirement. but the first one must be a blank image if you want to display the image whenwver the page is open.
Dec 21 '07 #3
Hi,
Forgot to say: color-spacer.gif is a blank image wich is replaced by globe.gif.
You can change the image as per your requirement. but the first one must be a blank image if you want to display the image whenwver the page is open.
Dec 21 '07 #4
sindhu
54
Hello Ranjan
Thanks alot for replying to my query...........:)
I used the code in my web page
I want a horizontl menu and each menu item has image before selcting and another image after selecting.
I mean to say there is a single image in which menu items are embedded .
when we click at any item position i want an image with that item highlighted to be uploaded.
Please help me in this.
thyanks alot again
Dec 21 '07 #5
Hello Ranjan
Thanks alot for replying to my query...........:)
I used the code in my web page
I want a horizontl menu and each menu item has image before selcting and another image after selecting.
I mean to say there is a single image in which menu items are embedded .
when we click at any item position i want an image with that item highlighted to be uploaded.
Please help me in this.
thyanks alot again
Hi,
For horizontal menu you can use style sheet to do that.
I have also used two images, one is blank(color_spacer.gif) image which is used before selecting and another image(globe_gif) after selecting.

I am giving you another simple code to do so.
The javascript code contain a function like this;
Expand|Select|Wrap|Line Numbers
  1. test_image = new Image();
  2. test_image1 = new Image();
  3. test_image2 = new Image();
  4. test_image3 = new Image();
  5. test_image4 = new Image();
  6.  
  7. function put_image(page)
  8. {
  9.     if(page == "home") document.test_image.src = "images/globe.gif";
  10.     if(page == "about_us") document.test_image1.src = "images/globe.gif";
  11.     if(page == "services") document.test_image2.src = "images/globe.gif";
  12.     if(page == "portfolio") document.test_image3.src = "images/globe.gif";
  13.     if(page == "contact_us") document.test_image4.src = "images/globe.gif";
  14. }
  15.  
And just use the html code below in every page;
Expand|Select|Wrap|Line Numbers
  1.       <ul id="menu">
  2.         <li><a href="index.html" title=""><img name="test_image" id="test_image" src="images/color_spacer.gif" align="top" />Home</a></li>
  3.         <li><a href="" title=""><img name="test_image1" id="test_image1" src="images/color_spacer.gif" align="top" />About Us</a></li>
  4.         <li><a href="" title=""><img name="test_image2" id="test_image2" src="images/color_spacer.gif" align="top" />Services</a></li>
  5.         <li><a href="" title=""><img name="test_image3" id="test_image3" src="images/color_spacer.gif" align="top" />Portofolio</a></li>
  6.         <li><a href="contact.html" title=""><img name="test_image4" id="test_image4" src="images/color_spacer.gif" align="top" />Contact</a></li>
  7.       </ul>
  8.  
FOR HORIZONTAL MENU USE in style sheet
Expand|Select|Wrap|Line Numbers
  1. #menu li {
  2.     list-style-type: none;
  3.     float: left;
  4.     margin: 0 8px;
  5.     height: 22px;
  6.  
I think this is what you want.
It just works like this. Initially you are having the images before selecting and when ever a page is selected you just do one thing you use the put_image function in the body tag like this;
IN HOME PAGE:
Expand|Select|Wrap|Line Numbers
  1. <body onload="javascript:put_image('home');">
  2.  
IN ABOUT_US PAGE:
Expand|Select|Wrap|Line Numbers
  1. <body onload="javascript:put_image('about_us');">
  2.  
and so on.....
Make sure the parameter you are passing should match with that used in the function and as per the page from where you are calling it i.e. in home page the parameter should be "home", etc.

Happy programming!
:)
Dec 21 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Naomi | last post by:
hello Could anyone please tell me if there is any way i can have the text "Menu Loading Please Wait..." appear while the js menu loads and then disappear after it loads ? hoping to hear some...
2
by: Sam Carleton | last post by:
I feel like a complete fool! I should know the answer to the Q: How do I load an image with JS and replace the default image? Some background: My final objective is to have a web site where it...
10
by: John Ortt | last post by:
Hi Everyone, I have created a Javascript menu for my site which uses frames. The first stage loads fine but I want two drill down menus ("About Me Menu" and "Projects Menu"). The pages load...
3
by: John Ortt | last post by:
Hi everyone, I posted with the title "Javascript Menu Not Loading Images" last week but I only posted the code as the site was on a corporate Intranet. Thanks to advice on the thread I have...
4
by: VR | last post by:
Hi, I am trying to have a menu item (which is an HTML img) to change as a mouse moves over it. So, my code looks something like this: <a onmouseover="ActivateImage('MyImage');"...
11
by: paladin.rithe | last post by:
I'm new to AJAX programming (and JavaScript), but I've been picking up the idea pretty quick. I have a question though. With the project that I'm working on, I would like to have the main page...
1
by: jennifer.lyell | last post by:
Hello, I am using Frontpage 2003 for a site design. I have 2 .swf files on the page as well as a menu bar (with cascading menus- created with All Web Menus Pro) that are on the page. When I...
3
by: aswathip | last post by:
My web application is developed in PHP. The Stylesheet for my Menu is written in a php file and CSS in javascript. When i access a page it shows the menu as such without any style. Main Menu...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.