document.write not working in html

I am try to write inside html document. this is my javascript code:

<script type="text/javascript">document.write("Hello World!")</script>

I am working with chrome and get the following error:

Uncaught TypeError: Object # has no method 'write'

I tried alert method and it worked.

EDIT: this is part of a project in scala/lift that also uses jquery if that may hint something. I suspect document object is redefined. is there a way to know that / to access the original one?


I had a similar problem, trying to embed the google maps api in my lift application. The script also uses document.write to load external libraries. The Google Chrome console stated that there is no function document.write.

The problem seems to be that XHTML does not allow document.write. ( http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite ) A solution could be to change the mime/type of your documents, e.g. by adding the following line to your Boot.scala

LiftRules.useXhtmlMimeType = false

More solutions are described in the link below http://scala-programming-language.1934581.n4.nabble.com/Google-Maps-API-V2-amp-V3-Ajax-Loading-td1981862.html


I am using VS code. I was doing the same, so what I did was make the JS file and shift the JS code there, meaning the ...

document.write("Printed by JavaScript.");

... part in main.js file. In index.html I just gave the path to my main.js file. It runs perfectly.

src = "main.js"