document.write not working in html

倾然丶 夕夏残阳落幕 提交于 2019-12-25 07:00:10

问题


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>


来源:https://stackoverflow.com/questions/7574943/document-write-not-working-in-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!