最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError:Cannot read property 'getElementById' frameset - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 1

Two things:

  1. Your code is relying on the automatic global that browsers create for elements that have ids on them, by using version 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:

  2. You may need vesrion.contentDocument rather than version.document in your currVersion = 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.

发布评论

评论列表(0)

  1. 暂无评论