我有一个简单的代码,每次有新版本时都会刷新主页,但它只适用于 Internet Explorer。在其他浏览器中我收到以下错误:
Uncaught TypeError: Cannot read property 'getElementById' of undefined line 24
这是我的代码:
<html>
<script type="text/javascript">
var num=0;
var currVersion;
var started=false;
function startLoad() {
var url= "version_telemark.html?"+ (++num);
window.version.navigate(url);
}
function endLoad() {
if (started) {
var newVersion = version.document.getElementById("version").value;
if (newVersion != currVersion) {
currVersion=newVersion;
var url = "telemark.html?"+ (++num);
window.pane.navigate(url);
}
}
}
function start() {
currVersion = version.document.getElementById("version").value;
started=true;
setInterval("startLoad()", 200);
}
</script>
<frameset onload="start()" cols="40%,100%">
<frame id="version" src="version_telemark.html"/>
<frame id="pane" src="telemark.html" />
</frameset>
</html>
在我的其他文件中,我只有我想要编辑的文件:
<input id="version" name="version" type="textbox" value="700">
我的另一个具有设计的文件只有表格,但我们不需要在其中添加任何内容。
最佳答案
有两件事:
您的代码依赖于浏览器为具有
id
的元素创建的自动全局,通过使用version
作为全局变量而无需声明或初始化它。也许由于某种原因,自动全局不适用于 IE 以外的浏览器。无论如何,我不喜欢依赖它们,因为太容易隐藏它们,所以我建议通过在脚本顶部附近添加以下内容来有意获取该元素:var version = document.getElementById("version");
但你对这个问题的评论表明这不是这个,而是下面的#2:
您的
currVersion = version.document.getElementById("version").value 中可能需要
和类似的行;也许是 JavaScript 的强大得令人好奇的vesrion.contentDocument
而不是version.document
;||
运算符:var versionDoc = version.document || version.contentDocument; // ...and then currVersion = versionDoc.getElementById("version").value; // ...and so on
旁注:为了让您自己以及在您之后维护代码的人保持理智,我还建议 version
frame 使用不同的
和其中的id
值版本
输入
。
关于javascript - 类型错误 :Cannot read property 'getElementById' frameset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27446752/