Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript API only firing once (strange place to break)

Status
Not open for further replies.

factotum

Technical User
May 29, 2002
48
0
0
US
I have the following functions that enable a page to "remember" choices in navigation (like Windows Explorer and the little plus/minus signs) from page to page. So, if you close a navigation subsection on pageA, and then click on over to pageB, the page remembers you had that section closed and recloses it onload.

I hide the layers using the trick of setting the display style to "none" (layer vanishes) or to an empty string "" (which is the default when the page is loaded-- and causes the layer to be displayed). My function also sets the image to the "expanded" or "collapsed" source.

I also have an API which does the browser work-arounds (with an extra function for NN4 (hat-tip: Peter-Paul Koch)). This is the simple area where my script dies. I have traced a layer id being read from the cookie, to the function to determine whether that layer should be set to a display of none, then passed to the function that changes these attributes-- ONCE! The second time through (reading from the cookie values in a for loop), the layer id is passed to the fuction, which determines it's state, which then goes to the API where it dies! I've tried Venkman, I can't figure out why a function would work great the first time, but not after that.

The effect of this error, is that the first layer value (id) on the cookie will properly be set to none (collapsed), but if you had closed more that two, the second to the nth layer would not collapse.

I apologize for the great length of this post, but I am at wits end. Thank you.

CODE:

Code:
// TOGGLE DISPLAY OF NAVIGATION
function toggleNavigation(object) {
	var x = new getObject(object);

	if (x.style.display == "") { // if layer is expanded
		onNavigation(object);
	} else if (x.style.display != "") { // if layer is collapsed
		offNavigation(object);
	}
}

function onNavigation(object) { // function to collapse a navigation section
	var obj = new getObject(object);
	var img = "tgl" + object.substr(3);
	var expandImg = newImage("./_library/images/navigation/expand.gif");
	var collapseImg = newImage("./_library/images/navigation/collapse.gif");
	var objImg = document.images[img];

		obj.style.display = "none";
		window.status = "[+] expand";
		objImg.src = expandImg.src;
		setCookie(object, "collapsed");
}

function offNavigation(object) { // function to expand a navigation section
	var obj = new getObject(object);
	var img = "tgl" + object.substr(3);
	var expandImg = newImage("./_library/images/navigation/expand.gif");
	var collapseImg = newImage("./_library/images/navigation/collapse.gif");
	var objImg = document.images[img];

		obj.style.display = "";
		window.status = "[-] collapse";
		objImg.src = collapseImg.src;
		deleteCookie(object);
}

// USE COOKIES TO REMEMBER NAVIGATION
function rememberNavigation() {
	var dc = document.cookie;
	var dc_array = dc.split(";");
	for (i=0; i < dc_array.length; i++) {
	//alert(i);
		if (dc_array[i].indexOf(&quot;nav&quot;) != -1) {
			var nav_array = dc_array[i].split(&quot;=&quot;);
			onNavigation(nav_array[0]);
		} else {
			continue;
		}
	}
}

// CROSS BROWSER OBJECTS
function getObject(name) {
  if (document.getElementById) {
  	this.object = document.getElementById(name);
	this.style = document.getElementById(name).style;
  } else if (document.all) {
	this.object = document.all[name];
	this.style = document.all[name].style;
  } else if (document.layers) {
   	this.object = getObjectNN4(document,name);
   	this.style = document.layers[name];
  }
}

function getObjectNN4(object, name) {
	var x = object.layers;
	var foundLayer;
	for (var i=0; i<x.length; i++) {
		if (x[i].id == name) {
		 	foundLayer = x[i];
		} else if (x[i].layers.length) {
			var tmp = getObjNN4(x[i],name);
		}
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}
 
This might help as well:

toggleNavigation()
Determines present state of passed layer and switches it (toggles) to the other state.

onNavigation()
The function that collapses the navigation section

offNavigation()
The function that expands the navigation section

rememberNavigation()
The fuction which goes through the document.cookie and looks for names with Nav in them (and then collapses those).

getObject()
The cross-browser API

getObjectNN4()
Finds layers within layers to return to NN4
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top