0

my overall goal is to load an image given user lattitude and longitude. However, my code has an error logic somewhere I assume (document.write("Dog") is never displaying to the screen) so I have to assume something is going wrong. The user is being prompted for a lat and longitude after clicking the button but afterwards nothing is happening. Any Ideas what might be going on? I'm a beginner so i am sure it is something simple.

<body>
<p>Click the button to demonstrate the prompt box.</p>
<button onclick="myFunction()">Try it</button>
   <script>
        function myFunction() {
            var x;
            var latittude = prompt("Please enter a lat");
            var longitude = prompt("please enter your longitude");
            var img = document.createElement('img');
            img.src = 'http://maps.googleapis.com/maps/api/streetview?size=400x400&location='+ latittude + ',' + longitude + '&heading=235&sensor=false';
            img.height = 200;
            img.width = 300;
            img.alt = alt;
            document.write(img.src);
            document.write("dog");
            document.getElementById('body').appendChild(img);
        }
</script>
 </body>
  • Don't use `document.write`, see the warning in [the spec](http://www.w3.org/TR/html5/dom.html#document.write%28%29). Use DOM methods instead. – Oriol Apr 09 '14 at 17:00

1 Answers1

1

img.alt = alt; but you have not defined a variable named alt.

document.getElementById('body') but there is no id with a value of 'body', to reference it use document.body.appendChild(img);.

Also unless this is just messing around for practice, take a look at: Why is document.write considered a “bad practice”?.

For future reference if you use the developer tools in your browser (F12) any script errors will be printed out in the console.

Community
  • 1
  • 1
Alex K.
  • 159,548
  • 29
  • 245
  • 267
  • Thanks. Figured it out based on this! Also, I had absolutely no idea about the developer tools. That is going to make the learning process much easier. – user3513466 Apr 09 '14 at 17:06