Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Introducing JavaScript Write Another Program

how can I write a function that will open an alert dialog with the message 'Warning!'

I am working on Javascript script tags, how can I write a function that will open an alert dialog with the message 'Warning!'

index.html
<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
</head>
<body>
    <script type="text/javascript" src="exercise1.js"></script>

</body>
</html>

2 Answers

Julian Gutierrez
Julian Gutierrez
19,201 Points

There is no need for the source attribute simply write your function inside of the script tags and call it.

<script>
  function sayWarning(){
    alert("Warning!"); 
  }
  sayWarning();
</script>
Vernon Neilly
Vernon Neilly
2,652 Points

Hi Kuo!

Writing a simple function is relatively easy!

a function is initiated with the 'function' keyword. Since you want to create a function to open an 'Alert' dialogue box, we can create your requested function like so:

function openAlert() { alert('This text will show in the alert box when your function is called.'); }

call the "openAlert" function created above like so:

openAlert();

Hope this helps. VN2