JavaScript的浏览器问题

问题描述:

我有这个脚本:JavaScript的浏览器问题

<html> 
<head> 
<script type="text/javascript"> 

function init(){ 

var extText = window.frames.messageTxt.document.body.lastChild.lastChild.data; 
extText = extText.replace(/[\r\n]/g," "); 
document.forms[0].nMessage.value = extText; 
} 

window.onload=init; 

</script> 
</head> 
<body> 
<iframe name='messageTxt' src='lineData.txt' style='display:none'></iframe> 
<form> 
<textarea name='nMessage'></textarea> 
</form> 
</body> 
</html> 

此代码打开一个文件(现在lineData.txt),并把它放在一个文本。此代码适用于Firefox和IE,但不适用于Chrome。

的问题是

window.frames.messageTxt.document.body.lastChild.lastChild.data; 

控制台告诉我,在该行:

Uncaught TypeError: Cannot read property 'body' of undefined

我该怎么做才能使之适用于Chrome?

+0

它适用于Chrome12中的我。你能设置一个不适合你的例子吗? – 2011-06-08 11:02:28

如果您在本地进行测试(例如file:// C:/.../test.html),您将无法正确访问Chrome中的帧/ iframe。

+0

我需要打开本地文件 – 2011-06-08 11:16:06

+1

@Diego Stamigni Chrome不会将所有本地文件视为来自同一个域;相反,它认为它们都截然不同。因此,默认安全策略不允许访问。如果您想这样做,则可以使用命令行选项“--allow-file-access-from-files”启动Chrome。在启动带有该标志的新程序之前,您必须确保您完全停止所有Chrome进程。 – Pointy 2011-06-08 11:25:08