admin管理员组文章数量:1427091
I have a simple code that refreshes the main page each time there is a new version, but it only works in Internet Explorer. In other browsers I get the following error:
Uncaught TypeError: Cannot read property 'getElementById' of undefined line 24
Here is my code:
<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>
and in my other file I only have that I wish to edit I have:
<input id="version" name="version" type="textbox" value="700">
and my other file that has the design just has tables but we wont need to add anything there.
I have a simple code that refreshes the main page each time there is a new version, but it only works in Internet Explorer. In other browsers I get the following error:
Uncaught TypeError: Cannot read property 'getElementById' of undefined line 24
Here is my code:
<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>
and in my other file I only have that I wish to edit I have:
<input id="version" name="version" type="textbox" value="700">
and my other file that has the design just has tables but we wont need to add anything there.
Share Improve this question edited Dec 12, 2014 at 15:33 hon2a 7,2245 gold badges45 silver badges59 bronze badges asked Dec 12, 2014 at 15:23 Henrique AlvesHenrique Alves 371 gold badge1 silver badge9 bronze badges 4-
Just FWIW,
frameset
is no longer part of the HTML specification. – T.J. Crowder Commented Dec 12, 2014 at 15:30 - Do you see any other errors when you run your app with a Developer Tools console open? (You might be prevented from accessing frame document for security reasons.) – hon2a Commented Dec 12, 2014 at 15:31
- On chrome it sends me this error: Uncaught TypeError: Cannot read property 'getElementById' of undefined – Henrique Alves Commented Dec 12, 2014 at 15:33
-
Well, the error you're getting means that
version.document
doesn't exist. – hon2a Commented Dec 12, 2014 at 15:34
1 Answer
Reset to default 1Two things:
Your code is relying on the automatic global that browsers create for elements that have
id
s on them, by usingversion
as a global variable without declaring or initializing it. Perhaps for some reason, the automatic global isn't working on browsers other than IE. I don't like to rely on them anyway, it's too easy to shadow them, so I suggest getting the element on purpose by adding this near the top of your script:var version = document.getElementById("version");
But your ment on the question suggests that it's not this, but #2 below:
You may need
vesrion.contentDocument
rather thanversion.document
in yourcurrVersion = version.document.getElementById("version").value;
and similar lines; perhaps JavaScript's curiously-powerful||
operator:var versionDoc = version.document || version.contentDocument; // ...and then currVersion = versionDoc.getElementById("version").value; // ...and so on
Side note: Just to keep yourself and whoever has to maintain the code after you sane, I'd also suggest different id
values for the version
frame
and the version
input
inside it.
本文标签: javascriptTypeErrorCannot read property 39getElementById39 framesetStack Overflow
版权声明:本文标题:javascript - TypeError:Cannot read property 'getElementById' frameset - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745490795a2660596.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论