-2

I have two separate files one with my html code and with my javaScript. What I am trying to do is create a function in javascript then call that function in the html. Both files are in the same folder along with the image. I'm new to both languages and to this site so please go easy;

I would really appreciate it if someone could help me please.`

JavaScript to load image below:

var menu =  new image();

menu.src = "Fitness App Entry Scrren.jpg"

function menuScreen(){
    document.getElementById("menu").getAttribute("src");
}

Html code to call function:

<!DOCTYPE html>
<html>
    <head>
    <body src="Functions.js">
    <script onload="menuScreen()"></script>
    </body> 
    <head>
</html>
jheimbouch
  • 929
  • 8
  • 19
HOAX
  • 5
  • 2

1 Answers1

0

What you are doing is against the rules of HTML. First of all, <body></body> should be outside of <head></head>. You should have <script></script> in either <head></head> or <body></body>. The <body> tag should have the onload attribute set to menuScreen(), and the <script> tag's src attribute should be set to Functions.js (as John Hascall said). By the way, John Hascall is right, there is no element with the ID of "menu" so it won't work unless you create a <div> or <iframe> with the specific ID and append the image to it in your Functions.js file.

So, your JavaScript code should look like this:

var menu = new Image(); // note that the constructor is capitalized
menu.src = "Fitness App Entry Screen.jpg";

// Create a <div> with the image within it.
var division = document.createElement("div");
division.setAttribute("id", "menu");
division.appendChild(menu);
document.body.appendChild(division);

function menuScreen() {
    division.getAttribute("src"); // you can use division because it has the id of menu
}

And here is your HTML code to run the page (according to the HTML5 specifications, note that):

<!DOCTYPE html>
<html>
    <head>
         <script src="Functions.js"></script>
    </head>
    <body onload="menuScreen()">
         <!-- Left intentionally blank -->
    </body>
</html>

Hopefully this will help you!

Obinna Nwakwue
  • 210
  • 4
  • 16