Last Comment Bug 663406 - document.write does not work in devtools scratchpad and console
: document.write does not work in devtools scratchpad and console
Product: Firefox
Classification: Client Software
Component: Developer Tools (show other bugs)
: Trunk
: All All
P3 normal with 1 vote (vote)
: ---
Assigned To: Nobody; OK to take it and work on it
: J. Ryan Stinnett [:jryans] (use ni?)
: 632796 (view as bug list)
Depends on: 632796
  Show dependency treegraph
Reported: 2011-06-10 08:56 PDT by Tobias (:Tobbi) Markus
Modified: 2016-02-29 12:32 PST (History)
17 users (show)
See Also:
Crash Signature:
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---

gdb backtrace from calling document.write(1) (4.33 KB, text/plain)
2011-09-06 15:52 PDT, David Chan [:dchan]
no flags Details

Description User image Tobias (:Tobbi) Markus 2011-06-10 08:56:14 PDT
When entering document.write("Something") inside the Scratchpad, you keep getting the not very descriptive error message 'Security error undefined'.

1. Go to Nightly > Web Developer > Scratchpad.
2. Enter document.write("test"); into the window and press Ctrl+R in order to run the code.
3. You get 'Security error undefined' in the Web Console window.

Expected behavior:
More descriptive error message. Web console window brings up 
[17:50:54.781] document.write("test");
[17:50:54.785] [Exception... "Security error"  code: "1000" nsresult: "0x805303e8 (NS_ERROR_DOM_SECURITY_ERR)"  location: "Web Console Line: 1"]
Comment 1 User image David Burns :automatedtester 2011-06-10 08:58:28 PDT
When doing this in Aurora I get

[16:45:11.093] An attempt was made to use an object that is not, or is no longer, usable

for doing the same steps as Tobbi
Comment 2 User image Anthony Hughes (:ashughes) [GFX][QA][Mentor] 2011-06-10 09:00:23 PDT
FWIW, the same security error is present in Firefox 4.0.1.
Comment 3 User image Kevin Dangoor 2011-07-27 11:13:11 PDT
(In reply to comment #1)
> When doing this in Aurora I get
> --
> [16:45:11.093] An attempt was made to use an object that is not, or is no
> longer, usable
> undefined:1
> for doing the same steps as Tobbi

This error seems reasonable to me.
Comment 4 User image Notlost 2011-09-04 11:15:58 PDT
I get the same nasty message as the reporter in the latest nightly...
Comment 5 User image David Chan [:dchan] 2011-09-06 15:52:23 PDT
Created attachment 558649 [details]
gdb backtrace from calling document.write(1)

This appears to be another bug from running WebConsole / ScratchPad in a sandbox. I've attached gdb output from running document.write(1) in Web Console. The problematic lines of code are in nsHTMLDocument.cpp [1]

1572   // Note: We want to use GetDocumentFromContext here because this document
1573   // should inherit the security information of the document that's opening us,
1574   // (since if it's secure, then it's presumably trusted).
1575   nsCOMPtr<nsIDocument> callerDoc =
1576     do_QueryInterface(nsContentUtils::GetDocumentFromContext());
1577   if (!callerDoc) {
1578     // If we're called from C++ or in some other way without an originating
1579     // document we can't do a w/o changing the principal of the
1580     // document to something like about:blank (as that's the only sane thing to
1581     // do when we don't know the origin of this call), and since we can't
1582     // change the principals of a document for security reasons we'll have to
1583     // refuse to go ahead with this call.
1585     return NS_ERROR_DOM_SECURITY_ERR;
1586   }

nsContentUtils::GetDocumentFromContext() returns NULL/0x0 due to nsJSUtils::GetDynamicScriptGlobal(cx) being NULL like in bug# 647727 . [2]

[1] -
[2] -
Comment 6 User image Blake Kaplan (:mrbkap) 2011-10-06 18:40:44 PDT
Out of curiosity, what's the intended result for a document.write from the web console or scratchpad? It's going to blow away the current document in favor of a new one (in fact, if you look at the stacktrace, it's that's throwing the exception). I suspect that this is pretty strongly related to bug 647727.
Comment 7 User image Kevin Dangoor 2011-10-07 08:30:05 PDT
FWIW, Firebug and Chrome both blow away the document. They also appear to both get into mostly useless states (the HTML panel in Firebug is empty, in Chrome it's got the old document). The new document is accessible via the console though.

So, it seems like that is the behavior people are expecting. I'd be curious to know what tasks people are trying to accomplish by doing document.write in those contexts...
Comment 8 User image Michael Ratcliffe [:miker] [:mratcliffe] 2012-01-05 08:50:06 PST
*** Bug 632796 has been marked as a duplicate of this bug. ***
Comment 9 User image Michael Ratcliffe [:miker] [:mratcliffe] 2012-01-05 08:51:48 PST
This is also an issue with the web console.
Comment 10 User image Rob Campbell [:rc] (:robcee) 2012-11-02 07:49:19 PDT
now I just get the more awesome:

Exception: The operation is insecure.

in Scratchpad. Nothing on the consoles.
Comment 11 User image Brandon Benvie [:benvie] 2013-08-02 11:42:42 PDT
This is back to producing the error "Exception: An attempt was made to use an object that is not, or is no longer, usable", maybe as a result of bug 825039 landing?
Comment 12 User image Frederik Braun [:freddyb] (off until March 6th) 2014-01-14 05:38:50 PST
This error is present in the toolbar as well as in scratchpad. Should we move it back to the generic devtools component?
I also think this bug (and the main problem) is about the document.write method not working, not necessarily about the error message itself.
I will change it accordingly.
Comment 13 User image baptx 2014-11-09 09:13:13 PST
Will this bug be fixed for Firefox console and scrathpad? Firebug console on Firefox or Chrome/Chromium console can use document.write without problem.
Comment 14 User image baptx 2016-02-29 11:57:40 PST
It seems fixed, I can use document.write(document.body.innerHTML) in the console of latest Firefox. Tested on Firefox 44.0 on Xubuntu 14.04 LTS and Tor Browser 5.0.4 (based on Mozilla Firefox 38.4.0). Anyone knows since which version it is fixed?
Comment 15 User image baptx 2016-02-29 12:05:01 PST
Also works on Firefox Scratchpad. PS: document.write(document.body.innerHTML) is useful to force HTML displays if Firefox is showing a blank web page when some assets are not loading but the source code has already been loaded. Here is an alternative I used when the bug was present: document.location = "data:text/html;charset=utf-8," + encodeURIComponent(document.body.innerHTML)
Comment 16 User image J. Ryan Stinnett [:jryans] (use ni?) 2016-02-29 12:32:19 PST
I agree it appears to work now.  Thanks for testing again!

Note You need to log in before you can comment on or make changes to this bug.