oshai
oshai

Reputation: 15355

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?

Upvotes: 0

Views: 24990

Answers (5)

user23185870
user23185870

Reputation: 1

Just take your script tag in the bottom of the body , the problem will be solved, I don't know why but it seems that when the script tag is in the head section it does this , despite using deffer attribute

Upvotes: 0

mtauqeer
mtauqeer

Reputation: 21

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"

Upvotes: 1

cmanteuffel
cmanteuffel

Reputation: 66

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

Upvotes: 5

Michael D. Irizarry
Michael D. Irizarry

Reputation: 6302

Did you try

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

Upvotes: 0

ilivewithian
ilivewithian

Reputation: 19692

Do you have another variable called document?

Upvotes: 0

Related Questions