2
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript Example</title>
<script>
function displayString() {
 return "<h1>Main Heading</h1>"
}
displayString();
document.write("Execute during page load from the head<br>");
</script>
</head>
<body>
<script>
 document.write("Execute during page load from the body<br>");
</script>
</body>
</html>

So this is my problem. No matter where I put the displayString(), the h1 just never seems to show up on the browser. Can anybody please help me see where I am wrong? I am new to JavaScript. Oh, and what I am trying to do is to call the function.

Cleptus
3,5304 gold badges31 silver badges38 bronze badges
asked Jun 8, 2020 at 12:04
3
  • 1
    Wow, two upvote for this question..!! Commented Jun 8, 2020 at 12:23
  • Yeah it's almost as if someone had asked for upvotes, maybe by people they know or those answering? Though that would be against the rules so, no, couldn't be that?! Commented Jun 8, 2020 at 12:35
  • You’re "properly" calling the function—you’re just not doing anything with the function’s return value, e.g. using it in a call to document.write to add it as content to the document. Commented Jun 8, 2020 at 13:08

2 Answers 2

1

You need to write the returned String to the document:

<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript Example</title>
<script>
function displayString() {
 return "<h1>Main Heading</h1>"
}
document.write(displayString());
document.write("Execute during page load from the head<br>");
</script>
</head>
<body>
<script>
 document.write("Execute during page load from the body<br>");
</script>
</body>
</html>

answered Jun 8, 2020 at 12:06
Sign up to request clarification or add additional context in comments.

1 Comment

1

No matter where I put the displayString(), the h1 just never seems to show up on the browser.

If you wish to add a new element to a document, several approaches are available:

  • document.write (effectively deprecated)
  • .innerHTML (sometimes useful, but can be slow)
  • DOM API - recommended approach

The recommended approach is to use the DOM API.

DOM stands for Document Object Model. Essentially it's the markup of your document represented as a tree-like structure of nodes. There are many DOM API functions which allow you to:

  • add
  • remove
  • append
  • prepend
  • insert
  • update

new DOM nodes.

Any DOM node may be added, removed or updated, including:

  • parent elements
  • child elements
  • sibling elements
  • element attributes
  • ids, classNames, classLists
  • custom data-* attributes
  • text nodes

Here is an example:

function displayMainHeading () {
 
 let mainHeading = document.createElement('h1');
 mainHeading.textContent = 'Main Heading';
 document.body.prepend(mainHeading);
}
displayMainHeading();
<p>Paragraph 1</p>
<p>Paragraph 2</p>


Further Reading

This is a good primer to get you started:

answered Jun 8, 2020 at 12:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.