Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Lifetime of browser.contentDocument

18 views
Skip to first unread message

Henrik Skupin

unread,
Nov 6, 2010, 9:59:02 PM11/6/10
to
Hi,

A couple of days ago I have discovered that some of our Mozmill tests
are failing. After some investigation, I have seen that it only happens
when the contentDocument of the tab has been cached and a new web page
gets loaded. The following snippet is a mozmill test and can be executed
by installing the extension [1].

> var testLifetime = function() {
> var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
> getService(Components.interfaces.nsIWindowMediator);
> var window = wm.getMostRecentWindow("navigator:browser");
>
> var browser = window.gBrowser.selectedBrowser;
> var document = browser.contentDocument;
>
> browser.addEventListener("load", function(event) {
> window.alert(document.getElementById("q")); // => null
> window.alert(browser.contentDocument.getElementById("q")); // => HTMLInputElement
> }, true);
>
> window.gBrowser.loadURI("http://www.mozilla.org");
> }

In such a situation it's not possible to retrieve an element from the
document. Instead I have to query for the document again.

Can someone please tell me what happens when a page gets loaded? Is the
document replaced with a new version holding the newly loaded web page?
Does it mean I always have to use browser.contentDocument.getElementById()?

Thanks in advance,

[1] https://addons.mozilla.org/de/firefox/addon/9018/

--
Henrik

Boris Zbarsky

unread,
Nov 6, 2010, 11:45:36 PM11/6/10
to
On 11/6/10 9:59 PM, Henrik Skupin wrote:
> Can someone please tell me what happens when a page gets loaded? Is the
> document replaced with a new version holding the newly loaded web page?

Yes (for most values of "a page gets loaded"; some things can behave
like loads in most ways but don't change the document object).

> Does it mean I always have to use browser.contentDocument.getElementById()?

If you don't explicitly update your cached document when navigation
happens, yes.

-Boris

Henrik Skupin

unread,
Nov 8, 2010, 12:31:34 AM11/8/10
to
Boris Zbarsky wrote on 11/6/10 8:45 PM:

>> Does it mean I always have to use browser.contentDocument.getElementById()?
>
> If you don't explicitly update your cached document when navigation
> happens, yes.

Right now we do not have a chance in Mozmill to update the cached
document, because we do not store the default view inside of our element
instances. So once the page has been reloaded even document.defaultView
is not accessible anymore.

But according to your comment I have created a patch that stores the
defaultView inside the element once it gets created. It's a workaround
for now but it doesn't change any interface for us. If you are
interested in, it is bug 610258.

https://bugzilla.mozilla.org/show_bug.cgi?id=610258

Thanks!

--
Henrik

0 new messages