Results 1 to 10 of 10

Thread: Change nav image to 'on' by page id

  1. #1
    Join Date
    Dec 2007
    New Jersey

    Change nav image to 'on' image by page id

    I am a newbie so sorry if some of this doesn't make sense. Here's what I'm trying to do:

    When a page loads, I want javascript to look for a page id. Example:
    <div id="page1"></div>

    Based on what the javascript finds, I want it to change a top nav image to the 'on' state to tell the user that they are on the current page.

    I plan to have 7 top nav 'tabs' on each page. So if I had 7 corresponding pages, in each page I would put <div id="page1"></div> for page1.html or <div id="page2"></div> for page2.html, etc.

    Here's my attempt:

    function changeTab() {
    var pageid = document.getElementById("page1");
    var img = document.getElementById("tabimage1");

    if (pageid == true) {

    Here's the HTML:

    Somewhere on the page, <div id="page1"></div>

    Then for the tab image:
    <li><a href="page1.html" target="_top"><img src="images/tabimage1_off.png" alt="page" name="tabimage1" border="0" id="tabimage1" /></a></li>

    My guess is that I will have to list maybe like this?
    var pageid = document.getElementById("page1");
    var pageid = document.getElementById("page2");
    var pageid = document.getElementById("page3");

    And I assume I will need to have a body onload images function to load the on tab images.

    I would REALLY appreciate your help, and if you do help me out, please know I started learning javascript yesterday so I really know nothing and am pretty much guessing at this.

    Thank you so much!
    Last edited by jasonbdesign; 12-07-2007 at 10:35 AM.

  2. #2
    Join Date
    Dec 2007
    New Jersey

    I Need Help!

    Hello? Anyone?

  3. #3
    Join Date
    Dec 2007
    New Jersey
    I made another attempt. . . . I really don't know much.

    <script type="text/javascript">
    var img = new Array();
    var i = 0;
    var docid = document.getElementById("tabimage1");

    function LoadImages()
    img[0] = new Image();
    img[0].src = "images/tab_off.png";
    img[1] = new Image();
    img[1].src = "images/tab_on.png";

    document.images[0].src = img[0].src;

    if(docid == "tabimage1") {
    i = 1;
    document.images[i].src = img[i].src;

    <body onload="LoadImages()">
    <img src="images/tab_off.png" id="tabimage1"/>

    Please Help!!!

  4. #4
    Join Date
    Oct 2007
    Western Massachusetts, USA
    Try this:
    function changeimage(imageid,imagesrc) { document.getElementById(imageid).src = imagesrc; }
    <img src="images/tab_off.gif" id="timg01" alt="Tab" width="256" height="256" onmouseover="changeimage('timg01','images/tab_on.gif')" onmouseoff="changeimage('timg01','images/tab_off.gif')">
    Computer programmer / web developer.

  5. #5
    Join Date
    Dec 2007
    New Jersey
    Actually I don't want to do a mouseover on and off. I don't know if that was very clear.

    I have seen a lot of mouse event stuff on here, but I want the image to change
    onLoad by looking for an id that tells which image to change to 'on.'

    Hopefully this clarifies things.

    Thank you for your reply though

  6. #6
    Join Date
    May 2007
    PHP Code:
    function changeTab(){
    //get the div element by its id
    var thediv=document.getElementById('divid');

    //get the image element by its id
    var theimg=document.getElementById('imgid');

    //now make the switch

    that's if you wanna hard-code everything in there...
    to be able to use the function in different places with different calls to it
    use this and call the function accordingly:

    PHP Code:
    function changeTab(divid,imgid,newsrc){
    //get the div element by its id
    var thediv=document.getElementById(divid);

    //get the image element by its id
    var theimg=document.getElementById(imgid);

    //now make the switch

  7. #7
    Join Date
    Dec 2007
    New Jersey

    Sort of

    That looks pretty simple. Is that PHP Code? The box says it is.

    I think that's pretty close. I'm going to try to explain it without using code. I think that may help.

    Say I have 7 pages. On each of those 7 pages, I have 7 nav tabs at the top that go to each one of those pages.

    Each page would have a div id in it according to what page it is:
    Example: page1.html has a div id="page1"

    Let's use page1 as an example.
    Page1 has all 7 tab images as the 'off' images initially.
    Example: page1button_off.jpg, page2button_off.jpg, etc.

    I'd like a script that looks to see (when any page loads) if it is page1, page2, page3, page4 , page5, page6 or page7.

    Let's say the script sees that it's page2. It would then find the page2 id,
    and use that as a basis to know which tab image to change to the 'on'
    image, in this case: page2button_off.jpg to page2button_on.jpg

    That's about the best I can explain it.

    I haven't seen anything like this, but imagine how valuable it would be! wouldn't have to go into every html page and change one of the buttons to the on or active state! The site I am trying to do this for has a lot of subnav pages under one of those 7 main tabs. So I am basically trying to avoid having to go into each one and change the top tab image to on.

  8. #8
    Join Date
    May 2007
    No, it's Javascript...I just used this forum's php tags to make it look nicer...

    I see what you mean, now...
    I wrote a script like this many months ago and submitted it to the javascript source and to this forum as well.

    here is what you were looking for:

    download the zip file and expand in to one directory
    the open up tabs.html

    this is a very inefficient way of doing this...
    with PHP includes, you can make a header and include it into your pages so you won't have to repeat your code 7 times like I did.

    good luck.
    I used colours to demo the script, but you can use the same script to switch bg images for the tabs.

  9. #9
    Join Date
    Dec 2007
    New Jersey


    Thanks gomisute
    I can't wait to try it out Monday morning. I'm sure other people will like being able to take a look at it too!

  10. #10
    Join Date
    May 2008

    This is what I have been looking for but with images

    I have been searching for this script for hours. Finally thought I found my solution. Only to see that the script does not define for images and I am not sure how to change it to reference images only? Please advise as I feel I am very close to the answer finally. And you were right, this would be extremely helpful to many people I am sure as I know this is something I have wanted to do for a long time. Thank you.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.11337 seconds
  • Memory Usage 2,943KB
  • Queries Executed 13 (?)
More Information
Template Usage (34):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (2)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (10)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates