Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    189
    Thanks
    38
    Thanked 3 Times in 3 Posts

    multiple functions called same time causing issue

    Hey all,

    I have an application.js file. The javascript is just a typical hide/show tabs
    effect, so when the user clicks on one link, the corresponding div is
    revealed and the other divs are hidden. Two pages require the
    javascript. However, while one page displays content of no tabs onload,
    the other page displays the content of the first tab onload. So the page
    that displays the content of no tab onload works fine. However, the page
    that displays the content of first tab onload is not working. I get the
    following error in FireBug:

    document.getElementById("tabs") is NULL
    var tabListItems = document.getElementById("tabs").childNodes;

    I think the issue is I'm calling multiple functions on page load (and
    hence since the one page doesn't have a tabs id it doesn't know what to
    do), even though I only need one function for each of the pages. But I
    would like to keep this tab effect in the same file. So I don't know what
    to do. Thanks for anyone's response:
    Code:
    window.onload = init;
    
    var tabLinks = new Array();
    var contentDivs = new Array();
    
    function init() {
      initProduct();
      initAbout();
    }
    
    function initProduct() {
      var tabListItems = document.getElementById("tabs").childNodes;
      for(var i=0; i < tabListItems.length; i++) {
        if(tabListItems[i].nodeName == "LI") {
          var tabLink = getFirstChildWithTagName(tabListItems[i], "A");
          var id = getHash(tabLink.getAttribute("href"));
          tabLinks[id] = tabLink;
          contentDivs[id] = document.getElementById(id);
          tabLinks[id].className = "";
          contentDivs[id].className = "tabContent hide";
          tabLinks[id].onclick = showTab;
          tabLinks[id].onfocus = function() { this.blur() };
        }
      }
    }
    
    function initAbout() {
      var tabListItems = document.getElementById("tabAbout").childNodes;
      for(var i=0; i < tabListItems.length; i++) {
        if(tabListItems[i].nodeName == "LI") {
          tabLink = getFirstChildWithTagName(tabListItems[i], "A")
          id = getHash(tabLink.getAttribute("href"));
          tabLinks[id] = tabLink;
          contentDivs[id] = document.getElementById(id);
        }
      }
      var i = 0;
      for(var id in tabLinks) {
        tabLinks[id].onclick = showTab;
        tabLinks[id].onfocus = function() { this.blur() }
        if(i == 0) {
          tabLinks[id].className = "selected"
        }
        i++
      }
      var i = 0;
      for(var id in contentDivs) {
        if(i != 0) {
          contentDivs[id].className = "tabContent hide";
        }
        i++
      }
    }
    
    function showTab() {
      document.getElementById("product").className = "tabContent hide"
      var selectedId = getHash(this.getAttribute("href"));
    
      for(id in contentDivs) {
        if(id == selectedId) {
          tabLinks[id].className = "selected";
          contentDivs[id].className = "tabContent";
        }
        else {
          tabLinks[id].className = "";
          contentDivs[id].className = "tabContent hide";
        }
      }
    
      return false;
    }
    
    
    
    function getFirstChildWithTagName(element, tagName) {
      for(var i=0; i < element.childNodes.length; i++) {
        if(element.childNodes[i].nodeName == tagName) {
          return element.childNodes[i];
        }
      }
    }
    
    function getHash(url) {
      var hashPos = url.lastIndexOf("#");
      return url.substring(hashPos + 1);
    }

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 342 Times in 338 Posts
    you can always check, if the element exists.
    PHP Code:
    function initProduct() {
      var 
    tabList document.getElementById("tabs");
      if (!
    tabList) {
        return 
    false;
      }
    // further code 
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Regular Coder
    Join Date
    Oct 2009
    Posts
    189
    Thanks
    38
    Thanked 3 Times in 3 Posts
    Should this have made the error go away and make all tabs but selected one display:

    Code:
    function init() {
    	initProduct();
    	initAbout();
    }
    
    function initProduct() {
    	var tabListItems = document.getElementById("tabs").childNodes;
    	if (!tabListItems) {
    		return false;
    	}
    	for(var i=0; i < tabListItems.length; i++) {
    		if(tabListItems[i].nodeName == "LI") {
    			var tabLink = getFirstChildWithTagName(tabListItems[i], "A");
    			var id = getHash(tabLink.getAttribute("href"));
    			tabLinks[id] = tabLink;
    			contentDivs[id] = document.getElementById(id);
    			tabLinks[id].className = "";
    			contentDivs[id].className = "tabContent hide";
    			tabLinks[id].onclick = showTab;
    			tabLinks[id].onfocus = function() { this.blur() };
    		}
    	}
    }
    Because the error still displays and the other tabs still do not hide. So I'm wondering if there is something else wrong?

    Thanks for any response.

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 342 Times in 338 Posts
    Quote Originally Posted by johnmerlino View Post
    Should this have made the error go away and make all tabs but selected one display:
    it should make the error go away.

    Quote Originally Posted by johnmerlino View Post
    Because the error still displays and the other tabs still do not hide. So I'm wondering if there is something else wrong?
    compare your code to mine and you’ll see what went wrong.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    johnmerlino (05-24-2010)

  • #5
    Regular Coder
    Join Date
    Oct 2009
    Posts
    189
    Thanks
    38
    Thanked 3 Times in 3 Posts
    Thanks, I see now.

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 342 Times in 338 Posts
    another possibility would be
    PHP Code:
    function initProduct() {
        try {
            var 
    tabListItems document.getElementById("tabs").childNodes;
        }
        catch (
    e) {
            
    console.log(e.message); // <-- not IE compatible
            
    return false;
        }
    // further code 
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Tags for this Thread

    Posting Permissions

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