Loading ...
Sorry, an error occurred while loading the content.

suggestion to avoid browser sniffing to getScroll in event.js

Expand Messages
  • Peter Michaux
    Hi, In event.js the get scroll methods use browser sniffing with the this.isIE bit of code. Browser sniffing is frowned upon in a big way. See these pages
    Message 1 of 1 , Sep 4, 2006
    • 0 Attachment
      Hi,

      In event.js the get scroll methods use browser sniffing with the
      ""this.isIE" bit of code. Browser sniffing is frowned upon in a big
      way. See these pages

      http://jibbering.com/faq/faq_notes/not_browser_detect.html
      http://www.quirksmode.org/js/support.html

      Fortunately there are ways to determine how much the page has been
      scrolled without browser sniffing. See the method on the following
      link

      http://jibbering.com/faq/faq_notes/not_browser_detect.html#bdScroll

      Using the conditionals from that link and the ideas from the section
      just before it on the same page, I suggest removing the following
      methods from the event.js file

      getPageX
      getPageY
      _getScrollLeft
      _getScrollTop
      _getScroll

      and then appending something similar to the following to the event.js file

      if(typeof window.pageXOffset == 'number') {
      YAHOO.util.Event.getPageX = function(e) {return e.clientX +
      window.pageXOffset;};
      YAHOO.util.Event.getPageY = function(e) {return e.clientY +
      window.pageYOffset;};
      } else if ((typeof document.compatMode == 'string') &&
      (document.compatMode.indexOf('CSS') >= 0) &&
      (document.documentElement) &&
      (typeof document.documentElement.scrollLeft=='number')){
      YAHOO.util.Event.getPageX = function(e) {return e.clientX +
      document.documentElement.scrollLeft;};
      YAHOO.util.Event.getPageY = function(e) {return e.clientY +
      document.documentElement.scrollTop;};
      } else if ((document.body) && (typeof document.body.scrollLeft == 'number')) {
      YAHOO.util.Event.getPageX = function(e) {return e.clientX +
      document.body.scrollLeft;};
      YAHOO.util.Event.getPageY = function(e) {return e.clientY +
      document.body.scrollTop;};
      }

      This will also be more efficient at runtime because the feature
      detecting has been done only once when the page loads, not every time
      the JavaScript calls getPageX().

      Netscape 4, with its Event.pageX, is one more consideration. However
      because event.js uses Function.call(), Netscape 4 is not supported
      anyway.

      I think removing the browser sniffing one-by-one were possible from
      Yahoo! UI is a good idea to make the library more robust.

      Peter
    Your message has been successfully submitted and would be delivered to recipients shortly.