When viewing a page without any mysite-links in IE7 I get the javascript error message: 'document.getElementById(...)' is null or not an object. This is caused by the first line in the mysiteHover function.

mysiteHover = function() {
sfEls = document.getElementById("mysite-links").getElementsByTagName("LI");

Comments

agentrickard’s picture

Two issues here, and a request.

1) Why is mysite_links.js being loaded on pages where no links exist? The mysite_link function wraps itself in the following IF:

  if ($type == 'node' && $node->nid) {

So on what pages (Drupal paths) are these occurring?

2) I do not have IE 7. So what is the recommended fix?

gustav’s picture

This is happening on node pages that use a template that does not show the node links. The template distributed with the organic groups module for use with organic group home pages is an example.

I think the fix would be to put a check into the javascript to see if the element actually exists. However I am not a javascript programmer.

agentrickard’s picture

Try replacing the existing code with this:

mysiteHover = function() {
  sfEls = document.getElementById("mysite-links").getElementsByTagName("LI");
  if (sfEls > 0) {
    for (var i=0; i<sfEls.length; i++) {
      sfEls[i].onmouseover=function() {
        this.className+=" mysite-hover";
      }
      sfEls[i].onmouseout=function() {
        this.className=this.className.replace(new RegExp(" mysite-hover\\b"), "");
      }
    }
  }
}
if (window.attachEvent) window.attachEvent("onload", mysiteHover);
gustav’s picture

The problem is that the getElementByID() is empty, so that an error is thrown already before sfEls is calculated. This code does not fix that.

agentrickard’s picture

OK. Not sure what do do about IE stupidity here. Try this.

mysiteHover = function() {
  var linksReady = '';
  var sfEls == '';
  linksReady = document.getElementById("mysite-links");
  if (linksReady != '') {
    sfEld = document.getElementById("mysite-links").getElementsByTagName("LI");
    if (sfEls != '') {
      for (var i=0; i<sfEls.length; i++) {
        sfEls[i].onmouseover=function() {
          this.className+=" mysite-hover";
        }
        sfEls[i].onmouseout=function() {
          this.className=this.className.replace(new RegExp(" mysite-hover\\b"), "");
        }
      }
    }
  }
}
if (window.attachEvent) window.attachEvent("onload", mysiteHover);
agentrickard’s picture

Or better. Go test these sample and report which one works.

http://www.htmldog.com/articles/suckerfish/dropdowns/

gustav’s picture

This comes close. However you do not want to check for the empty string. Instead you want to check that the variable is not Null. Apparently, javascript makes a distinction there, unlike PHP. Also there should only be a single = in var sfE1s = ''

The following works in IE7:

mysiteHover = function() {
  var linksReady = '';
  var sfEls = '';
  linksReady = document.getElementById("mysite-links");
  if (linksReady) {
    sfEld = document.getElementById("mysite-links").getElementsByTagName("LI");
    if (sfEls) {
      for (var i=0; i<sfEls.length; i++) {
        sfEls[i].onmouseover=function() {
          this.className+=" mysite-hover";
        }
        sfEls[i].onmouseout=function() {
          this.className=this.className.replace(new RegExp(" mysite-hover\\b"), "");
        }
      }
    }
  }
}
if (window.attachEvent) window.attachEvent("onload", mysiteHover);

However, as I said, I don't know javascript, and so I don't know whether the above will work in all browsers.

agentrickard’s picture

Status: Active » Needs review

I am rather shocked that it worked in IE7. My guess was that it did not like being asked to evaluate the TagName after not finding the parent Element.

It should be fine in other browsers. Will test.

This affects 5.x.2 as well.

Thanks.

agentrickard’s picture

Status: Needs review » Reviewed & tested by the community

Works in FF / Mac and Safari / Mac. So considering harmless.

agentrickard’s picture

Status: Reviewed & tested by the community » Fixed

Committed to HEAD. Thanks for the debugging support!

agentrickard’s picture

Backported to 5.x.2.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.