[removed] not working in html

2019-09-08 06:58发布

问题:

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?

回答1:

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



回答2:

Do you have another variable called document?



回答3:

Did you try

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


回答4:

I am not sure why this happened, maybe its related to the phase the page load was on. as a workaround I added a div with id hellowworlddiv and the following worked:

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