使用 CoffeeScript 编写 document.write
我知道我可能做错了,因为如果通过 尝试 Coffeescript 功能 尝试这样做,它会起作用,但是令人惊讶的是,它在我的示例中没有发出任何结果:
<!--http://f.cl.ly/items/1u3Q3W101U2T18162v0V/test.html-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Page Title</title>
<script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"></script>
</head>
<body>
<script type="text/coffeescript" >
document.write "<h2>TEST</h2>"
</script>
</body>
</html>
document.write
方法似乎没有向正文输出任何内容,在这种情况下,console.log 工作正常,但 document 则不然。 write
即使在尝试使用 onload
处理程序运行脚本(就像我在 javascript 中使用的那样)
var loaded = function(){
alert("hello");
}
document.addEventListener('DOMContentLoaded', loaded);
,但随后在 Coffeescript 中使用,因为
loaded = ->
alert "hello"
document.addEventListener "DOMContentLoaded", loaded
与 javascript 版本相反,事件方法似乎都没有被触发
任何人都可以提供帮助我知道发生了什么事吗?
感谢
更新,
如果在加载页面后运行控制台,我可以毫无问题地执行以下操作:
CoffeeScript.eval('document.write "<h1>testing</h1>"')
但仍然想知道为什么页面本身没有自动显示
适用于 Firefox 和 Chrome,但不适用于 Safari
如果使用,页面似乎不会显示Safari 5.0.3
I know I am probably doing this wrong because if trying this through try coffeescript feature it works but surprisingly it doesn't emit any result on my example:
<!--http://f.cl.ly/items/1u3Q3W101U2T18162v0V/test.html-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Page Title</title>
<script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"></script>
</head>
<body>
<script type="text/coffeescript" >
document.write "<h2>TEST</h2>"
</script>
</body>
</html>
The document.write
method doesn't seems to output anything to the body, in this case console.log works fine but not document.write
Even after trying to run the script with a onload
handler like I use in javascript
var loaded = function(){
alert("hello");
}
document.addEventListener('DOMContentLoaded', loaded);
but then in coffeescript as
loaded = ->
alert "hello"
document.addEventListener "DOMContentLoaded", loaded
it seems neither the event method is being fired as opposed to javascript version
Anyone could help me find out what is happening?
Thanks
UPDATE
if running the console after the page is loaded I can get the following to work without problem:
CoffeeScript.eval('document.write "<h1>testing</h1>"')
but still wondering why the page itself is not showing automatically
Works on Firefox and Chrome but not in Safari
It seems the page is not showing if using Safari 5.0.3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 CoffeeScript 一无所知,但不要使用 document.write。这是邪恶的:http://javascript.crockford.com/script.html
使用createElement和appendChild /insertBefore 相反:
document.write 在 Safari 中也有问题。
I don't know anything about CoffeeScript, but don't use document.write. It is evil: http://javascript.crockford.com/script.html
Use createElement and appendChild/insertBefore instead:
document.write has problems in Safari as well.
这是一个难题,但经过调查,我得到了答案:
coffee-script.js 的工作方式是它查找并运行带有
type="text/ 的脚本文档加载后,coffeescript”
。对于 Safari,这意味着相当于
默默地失败。请注意,使用 Erlend 描述的
document.createElement
方法或使用 jQuery 等库进行插入可以正常工作。由于这在 Chrome 中有效,所以我将其称为 Safari 错误。但这个故事的真正寓意是:不要使用 document.write。
This is a humdinger, but after investigating, I've got your answer:
The way
coffee-script.js
works is that it looks for, and runs, scripts withtype="text/coffeescript"
after the document has loaded. In the case of Safari, that means thatis equivalent to
which silently fails. Note that making the insertion with the
document.createElement
method described by Erlend, or with a library like jQuery, will work fine.Since this works in Chrome, I'd go ahead and call it a Safari bug. But the real moral of the story is: Don't use document.write.