Jason Lemon
Jason Lemon

Reputation: 33

Javascript function call from html

I'm doing javascript homework(total newb) and the study guide says...

Add document.write() statements around each line of HTML code (p. 53 gives an example) Be sure to keep the code organized and easy to read. Keep in mind that single (') and double (") quotes must be nested to avoid errors.

I've done that here.

    function displayHeader() {

        document.write("<h1>

            <img src="images/PeteBanner.jpg" alt="Pistol Pete" />

            Jason Lemon's Javascript Website! 

            <img src="images/PeteBanner.jpg" alt="Pistol Pete" /></h1>;
    };

When I go to the header section of the html file...I'm supposed to call the function. I referenced the javascript file in the head section. Here is what I'm putting in the header section. It's not working. I know my code is way off.

Upvotes: 2

Views: 70

Answers (3)

oliverwebr
oliverwebr

Reputation: 627

You need to concat the strings or write all in one line. Like this it would look like if you concatenate the strings.

function displayHeader() {
  document.write(
    "<h1> "
    + "<img src = 'images/PeteBanner.jpg' alt = 'Pistol Pete' / >"
    + "Jason Lemon 's Javascript Website!" 
    + "<img src = 'images/PeteBanner.jpg' alt = 'Pistol Pete' / > </h1>"
  );
};

Upvotes: 0

Amit Sheen
Amit Sheen

Reputation: 249

Use one line.

function displayHeader() {

    document.write('<h1><img src="images/PeteBanner.jpg" alt="Pistol Pete" />Jason Lemon\'s Javascript Website! <img src="images/PeteBanner.jpg" alt="Pistol Pete" /></h1>')
};

Upvotes: 1

adeneo
adeneo

Reputation: 318342

It should look more like this ...

function displayHeader() {
    document.write(
        '<h1>' +
            '<img src="images/PeteBanner.jpg" alt="Pistol Pete" />' +
             'Jason Lemon\'s Javascript Website!' +
             '<img src="images/PeteBanner.jpg" alt="Pistol Pete" />' +
        '</h1>';
    )
}

... but using document.write is generally bad practice, but if that's what the assigment says, I guess it's okay, just know that you shouldn't be using it.

To call the function after you've included the script in your HTML, you can do

<script type="text/javascript">

    displayHeader()

</script>

Upvotes: 1

Related Questions