1

So I just started with HTML and JavaScript and I thought it would be fun to experiment with iFrames.

But I ran into a problem I can't seem to solve: Uncaught TypeError: Cannot read property 'value' of null

This is the code:

<!DOCTYPE html>

<html>
    <head>
        <meta charset="UTF-8">
        <script>
            var link = document.getElementById("LinkJ");
            var button = document.getElementById("ButtonJ");
            var surf = document.getElementById("SurfJ");

            function PressButton(){
                surf.src = link.value;
            }
        </script>
    </head>

    <body>
        URL:
        <input name="LinkN" id="LinkJ" type="url" value="http://www.bing.com/"></input>
        <button name="ButtonN" id="ButtonJ" onclick="PressButton()">Surf</button>

        <br><br>

        <iframe name="SurfN" id="SurfJ" style="position: absolute; height: 80%; width: 90%" src="http://www.bing.com/">
        </iframe>
    </body>
</html>
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Bram
  • 19
  • 5

1 Answers1

1

Your script is executing before the DOM is rendered. Move your script at the end of your html or after the DOM elements you are using.

Or you can also update your script to following

<script>
    function PressButton(){
        document.getElementById("SurfJ").src = document.getElementById("LinkJ").value;
    }
</script>
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59