Re: scrollIntoView for XML documents

Martin Honnen <[email protected]>
Newsgroups gmane.comp.mozilla.devel.xml
Organization Liberty Development
Message-ID <[email protected]>

conor325 wrote:

> for HTML and XHTML (http://www.w3.org/1999/xhtml), the function
> scrollIntoView allows you to pick an element in a document and make it
> visible in a browser frame or window. However our texts are XML files,
> specifically XHTML 2 with "section" and "h" and other tags specific to
> that standard. Firefox treats them, as it should, as "plain XML" but it
> doesn't support scrolling to a point for such documents ... or does it?
> Is there a way to dynamically make an arbitrary element in a displayed
> XML document visible?

You could implement that with script if arbritary XML elements had 
offsetLeft/Top/Parent properties but they don't have that with Mozilla.

Opera 8.00 beta seems to expose those properties to XML elements so I 
have a simple test case with Opera 8.00 beta where you can do

     function getPageCoords (element) {
       var coords = null;
       if (typeof element.offsetLeft != 'undefined') {
         coords = { x: 0, y: 0 };
         while (element) {
           coords.x += element.offsetLeft;
           coords.y += element.offsetTop;
           element = element.offsetParent;
         }
       }
       return coords;
     }

     function scrollElementIntoView (element) {
       /* desirable but does not work
       if (typeof element.scrollIntoView != 'undefined') {
         element.scrollIntoView(true);
       }
       */
       var pageCoords = getPageCoords(element);
       if (pageCoords) {
         window.scrollTo(pageCoords.x, pageCoords.y);
       }
     }

The coordinates are not precise it seems as the scroll position seems a 
bit wrong but the properties are there.


-- 

	Martin Honnen
	http://JavaScript.FAQTs.com/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.