SharePoint Javascript Bug

txrandom

Diamond Member
Aug 15, 2004
3,773
0
71
Our SharePoint site currently has a bug where some of the content is set to "display:none" when being viewed in Firefox and possibly other non-IE browsers. The bug stems from the code below:

<script language=javascript>
function MSOLayout_MakeInvisibleIfEmpty() {
var allElements=document.getElementsByName("_invisibleIfEmpty");
var agt=navigator.userAgent.toLowerCase();
var isNav=((agt.indexOf('mozilla')!=-1)&&((agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible')==-1)));
var isIE=(agt.indexOf("msie")!=-1);
for (var curElement=0; curElement < allElements.length; curElement++) {
if ((isIE && allElements[curElement].childNodes.length==0) || (isNav && allElements[curElement].childNodes.length <=1)) {
allElements[curElement].style.display="none";
}
}
}
</script>

For some reason, the element is being set to "display: none" if the length of its child nodes are less than or equal to 1. The equal to bugs me. Why should the element be hidden if there is a single child node?

For example, some markup like this would be hidden when that function is run:

<div id="example" name="_invisibleIfEmpty">
Hello World!
</div>

If I output the child node length of example in IE6 and Firefox 3, they both equal 1.

<div id="example" name="_invisibleIfEmpty">
Hello World! <p>Extra Hello World!</p>
</div>

Using this the lengths both equal 2.

It appears that child node length works the same if both IE and Firefox (and Mozilla as a whole?), so why is the Javascript hiding elements with child node length of 1 or less only in mozilla browsers?
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126

this definitely looks like a bug in the javascript. Why did MS make the condition for mozilla browsers childNodes.length <= 1. Maybe a previous version of mozilla reported childNodes.length = 1 even when the childNodes was empty? I guess this is why we don't let our users use anything but IE in sharepoint.

Can you change it to :
(isNav && allElements[curElement].childNodes.length ==0))
 

txrandom

Diamond Member
Aug 15, 2004
3,773
0
71
Originally posted by: KB

this definitely looks like a bug in the javascript. Why did MS make the condition for mozilla browsers childNodes.length <= 1. Maybe a previous version of mozilla reported childNodes.length = 1 even when the childNodes was empty? I guess this is why we don't let our users use anything but IE in sharepoint.

Can you change it to :
(isNav && allElements[curElement].childNodes.length ==0))

@PhatoseAlpha

The code actually decides between using core Javascript files named IE5.0, IE5.5, and nonIE. The code for that particular function is actually the same in everyone.

The site is fairly new and running on SharePoint 2007. It just went live a few days ago. We did end up just overriding the function in the HTML itself of the master pages. We didn't want to overwrite the core Microsoft files.

@KB

I believe you are right KB. The only mainstream Mozilla browser nowadays is Firefox, right? I'm pretty sure there wouldn't be a difference between Firefox and other Mozilla browsers since they all use the Mozilla tool kit. I think it's just something Microsoft forget to remove.

Although even if Mozilla did use to default child node length to 1, wouldn't that still hide elements that really do just have a child node length of 1 as well?

I just googled to see if there was previously a Mozilla bug with it...and this thread shows up at the top. :)

Just googled using Netscpae instead of Mozilla. Looks like there is/was a problem with child node length and tables, which SharePoint uses a ton of. Based on when SharePoint 2007 was developed, Netscape was actually common, so this makes sense now. I wish they would update it, but they probably prefer to keep Firefox broken.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,816
75
If it's a bug in the Javascript, you could make a GreaseMonkey script to fix it in Firefox.
 

txrandom

Diamond Member
Aug 15, 2004
3,773
0
71
It's a bug in Microsoft's Javascript code not the Javascript language. We'd rather fix the issue from server side rather than a client side add on.
 

txrandom

Diamond Member
Aug 15, 2004
3,773
0
71
Ken g6, I've heard about Greasemonkey before but never really knew what it was about. It would be a simple fix for our problem, but I don't think our company would want to install that. On the other hand, this will be cool for a lot of personal scripting things I can come up with.