Jump to content

Script tag needed...?


patricia_grace
 Share

Recommended Posts

In the beginning of the JavaScript tutorial, there are several examples of working with JavaScript that don't include any script tags. I was able to duplicate the examples with my own text editor and browser, also.

 

However, later in the tutorial it is stated "In HTML, JavaScript code must be inserted between <script> and </script> tags."

 

It is a bit confusing, and I am wondering why the script tags weren't needed in the first few examples, such as the one below:

 

<!DOCTYPE html>
<html>
<body>
<h1>What Can JavaScript Do?</h1>
<p id="demo">JavaScript can change HTML content.</p>
<button type="button" onclick="document.getElementById('demo').innerHTML = 'Hello JavaScript!'">Click Me!</button>
</body>
</html>
Which runs fine without the script tags. I was able to duplicate it with my own text editor and browser.
Thanks in advance for any help offered!
patricia
Link to comment
Share on other sites

They are wrong when they say it must be embedded in <script> tags. It also can be in an even attribute.

 

But as a general rule, you use <script> tags for Javascript. I discourage the use of event attributes.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...