[thelist] getElementsByClass prob

ben morrison morrison.ben at gmail.com
Tue Jul 11 05:13:15 CDT 2006


On 7/11/06, XtiaN <indotekken at yahoo.com> wrote:
> hi guys, i've problem here.. i want to make pop up window from my a href links using unobtrusive js.
>  but it doesn't work...
>
>  code in my html
>  <head>
>  <script type="text/javascript">
>  addLoadEvent(function() {
>  findBT2('btnym','/member/includes/popupym.html','200','100');
>  findBT2('btnmsn','/member/includes/popupmsn.html','200','100');
>  }

Looks like your addLoadEvent is missing a bracket

addLoadEvent(function() {
 	 /* more code to run on page load */
});


>  </script>
>  </head>
>  ....
>  <a href="#" class="btnym">link</a>
>  <a href="#" class="btnmsn">link</a>
>  ...


This is not unobtrusive, you should keep the href in the link.
Depending on your code it could be better to place these links into a
list or a div and use getElementById


>
>  function addLoadEvent(func) {
>    var oldonload = window.onload;
>    if (typeof window.onload != 'function') {
>      window.onload = func;
>    } else {
>      window.onload = function() {
>        if (oldonload) {
>          oldonload();
>        }
>        func();
>      }
>    }
>  }
>
>  //dustindiaz
>  function getElementsByClass(searchClass,node,tag) {
>      var classElements = new Array();
>      if ( node == null )
>          node = document;
>      if ( tag == null )
>          tag = '*';
>      var els = node.getElementsByTagName(tag);
>      var elsLen = els.length;
>      var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");
>      for (i = 0, j = 0; i < elsLen; i++) {
>          if ( pattern.test(els[i].className) ) {
>              classElements[j] = els[i];
>              j++;
>          }
>      }
>      return classElements;
>  }
>
>  function findBT2(btnClass,what,width,height){
>      var btn = getElementsByClass(btnClass,document,'*');
>      //alert(btn);
>      btn.onclick = function() {
>          window.open(what, 'title', 'width='+width+',height='+height+'');
>          return(false);
>      }
>  }

btn.onclick will not work, as getElementsByClass returns an array. You
could use btn[0].onclick and grab the href from it -  although this
isn't very scaleable.

ben



More information about the thelist mailing list