views:

140

answers:

2

I'm developing firefox sidebar that interacts with the main browser window. When a button within the sidebar is clicked, I need to be able to determine how far down the current page the user has scrolled.

Anybody have any ideas? I can't seem to find the right combination.

A: 

I think the scrollTop and scrollHeight attributes should help you. You can combine it with the clientHeight attribute.

Pseudo code :

scrollPercent = 100
if(clientHeight <= scrollHeight) {
    scrollPercent = (scrollTop / (scrollHeight - clientHeight)) * 100
}
lithorus
The following will return me the client height fine:window.QueryInterface(Components.interfaces.nsIInterfaceRequestor) .getInterface(Components.interfaces.nsIWebNavigation) .QueryInterface(Components.interfaces.nsIDocShellTreeItem) .rootTreeItem .QueryInterface(Components.interfaces.nsIInterfaceRequestor) .getInterface(Components.interfaces.nsIDOMWindow) .gBrowser.selectedBrowser.clientHeightHowever calling scrollHeight on the same object always returns a value equal to clientHeight, and calling scrollTop always returns 0.Apologies for the formatting :/
A: 

Found the answer for anyone else that may be looking for it:

window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  .getInterface(Components.interfaces.nsIWebNavigation)
  .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
  .rootTreeItem.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  .getInterface(Components.interfaces.nsIDOMWindow)
  .gBrowser.selectedBrowser.contentWindow.scrollY