PDA

View Full Version : SDMenu on first page



Ebbs
10-31-2008, 10:44 AM
1) Script Title: Slashdot

2) Script URL (on DD):

3) Describe problem: http://www.dynamicdrive.com/dynamicindex1/slashdot.htm

I build my menu structure dynamically in code behind after retrieving the relevant data from sql. So, after a client logs onto the system, the menu structure changes to that products he/she is paying for.

On the logon page, though, I get a javascript error that says "'this.submenu[...]' is null or not an object.". No where else after the client has logged on does this error appear.

Here is the code:

I think the problem is where I marked the code in red. I am new to building controls dynamically, so I would really appreciate any help.

Thanx in advance :)


function SDMenu(id) {
if (!document.getElementById || !document.getElementsByTagName)
return false;
this.menu = document.getElementById(id);
this.submenus = this.menu.getElementsByTagName("div");
this.remember = true;
this.speed = 3;
this.markCurrent = true;
this.oneSmOnly = false;
}
SDMenu.prototype.init = function() {
var mainInstance = this;
for (var i = 0; i < this.submenus.length; i++)
this.submenus[i].getElementsByTagName("span")[0].onclick = function() {
mainInstance.toggleMenu(this.parentNode);
};
if (this.markCurrent) {
var links = this.menu.getElementsByTagName("a");
for (var i = 0; i < links.length; i++)
if (links[i].href == document.location.href) {
links[i].className = "current";
break;
}
}
if (this.remember) {
var regex = new RegExp("sdmenu_" + encodeURIComponent(this.menu.id) + "=([01]+)");
var match = regex.exec(document.cookie);
if (match) {
var states = match[1].split("");
for (var i = 0; i < states.length; i++)
this.submenus[i].className = (states[i] == 0 ? "collapsed" : "");
}
}
};
SDMenu.prototype.toggleMenu = function(submenu) {
if (submenu.className == "collapsed")
this.expandMenu(submenu);
else
this.collapseMenu(submenu);
};
SDMenu.prototype.expandMenu = function(submenu) {
var fullHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
var links = submenu.getElementsByTagName("a");
for (var i = 0; i < links.length; i++)
fullHeight += links[i].offsetHeight;
var moveBy = Math.round(this.speed * links.length);

var mainInstance = this;
var intId = setInterval(function() {
var curHeight = submenu.offsetHeight;
var newHeight = curHeight + moveBy;
if (newHeight < fullHeight)
submenu.style.height = newHeight + "px";
else {
clearInterval(intId);
submenu.style.height = "";
submenu.className = "";
mainInstance.memorize();
}
}, 30);
this.collapseOthers(submenu);
};
SDMenu.prototype.collapseMenu = function(submenu) {
var minHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
var moveBy = Math.round(this.speed * submenu.getElementsByTagName("a").length);
var mainInstance = this;
var intId = setInterval(function() {
var curHeight = submenu.offsetHeight;
var newHeight = curHeight - moveBy;
if (newHeight > minHeight)
submenu.style.height = newHeight + "px";
else {
clearInterval(intId);
submenu.style.height = "";
submenu.className = "collapsed";
mainInstance.memorize();
}
}, 30);
};
SDMenu.prototype.collapseOthers = function(submenu) {
if (this.oneSmOnly) {
for (var i = 0; i < this.submenus.length; i++)
if (this.submenus[i] != submenu && this.submenus[i].className != "collapsed")
this.collapseMenu(this.submenus[i]);
}
};
SDMenu.prototype.expandAll = function() {
var oldOneSmOnly = this.oneSmOnly;
this.oneSmOnly = false;
for (var i = 0; i < this.submenus.length; i++)
if (this.submenus[i].className == "collapsed")
this.expandMenu(this.submenus[i]);
this.oneSmOnly = oldOneSmOnly;
};
SDMenu.prototype.collapseAll = function() {
for (var i = 0; i < this.submenus.length; i++)
if (this.submenus[i].className != "collapsed")
this.collapseMenu(this.submenus[i]);
};
SDMenu.prototype.memorize = function() {
if (this.remember) {
var states = new Array();
for (var i = 0; i < this.submenus.length; i++)
states.push(this.submenus[i].className == "collapsed" ? 0 : 1);
var d = new Date();
d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000));
document.cookie = "sdmenu_" + encodeURIComponent(this.menu.id) + "=" + states.join("") + "; expires=" + d.toGMTString() + "; path=/";
}
};

ddadmin
10-31-2008, 11:34 PM
Please post a link to the page on your site that contains the problematic script so we can check it out.

Ebbs
11-02-2008, 08:49 AM
The update to our website hasn't been published yet, so only the developers at out company can see the problem. I was asked to fix the problem before we can deploy.

Would it help if I post the html source so you can review it? I'll only be able to do that on Monday...

Ebbs
11-17-2008, 08:05 AM
Sorry for taking this long, but I came down with some tonsillitus and was mandown for a while.

Anyway, our website haven't been published yet, so I can't give you a link for it yet. I have though, sortof figured out this morning what is going wrong and I think it has something to do with the way the menu uses cookies.

When the code enters the SDMenu.prototype.init part of the script, it keeps on returning a javascript error at the second if statement, specifically the line which reads: "this.submenu[i].classname = (states[i] == 0 ? "collapsed" : "" );" and the error that I'm getting is that this.submenu isn't an object or is null. This doesn't happen when I have this.remember set to false.

The reason I think this is happening is because of the way our website works and how the menu gets constructed: At our logon page there is only one span which gets expanded/contracted, but after the client logs on, this changes to as many as 7 spans, depending on the product choices the client bought from us. The way I do this is by building a string for the innerhtml of the sdmenu in codebehind.

So, when the client logs out/closes the browser and comes back later to the page, the I found the states.length (caused by the cookie??) is thill set to wrong length of spans from the previous page, not just 1 for the login page and thus something goes wrong in that part of the code.

I think the solution to this would be if I can reset or clear the cookies for the menu as the client logs in/out, before that page gets build. This is the part I don't know how to do and would really appreciate any help on this matter.

If there is any questuons, please feel free to ask.

Thanx again in advance :)

Ebbs
11-19-2008, 01:25 PM
I resolved this problem. I clear the spesific cookie in code behind in the preinit event.