tags:

views:

85

answers:

1

If I have an RichTextArea like this :

RichTextArea rta = new RichTextArea();
rta.setHTML("<p id=\"foo\">Foo</p>....<p id=\"bar\">Bar</p>");

If I extend the RichTextArea class, how would be the proper way (cross-browser wise) to write a scrollTo() method?

Ex:

class RichTextAreaExt extends RichTextArea {
   ...
   /**
    * This method should be called only when the widget has properly been attached
    * @param id String the HTML element id within the RichTextArea to scroll to
    */
   public native void scrollToElement(String id) /*-{
       var cWin = [email protected]::getElement()().contentWindow;
       var el = cWin.document.getElementById(id);
       if (el) {
           cWin.scrollTo(el.offsetLeft,el.offsetTop);
       }
   }-*/;       ...
}

This seems to work in some browsers, but I don't have all of them to test, so inputs are welcome!

Thanks!

+1  A: 

http://code.google.com/p/doctype/wiki/WindowScrollMethod - seems to be ok across browsers. http://code.google.com/p/doctype/wiki/ArticleOffsetLeftAndFriends - seems to be inconsistent.

Gipsy King
window.scroll() seems to be an alias of window.scrollTo() (http://www.w3schools.com/jsref/met_win_scrollto.asp). As for the second link, if I understand the article, unless I have a weird layout inside my RichTextArea, I should not have too much trouble scrolling (near) to the desired element.
Yanick Rochon
@Yanick Rochon: I guess you will either have to test for offsetleft and top across browsers for yourself, or use some framework like jquery (in your iframe). If you find a working solution, it would be nice if you post it here.
Gipsy King
closing this question now. Thank you for your answer
Yanick Rochon