Forgot username/password? Don't have an account?
Login with Facebook
Home » Courses » JavaScript 101 » The Script Tag

JavaScript 101

  1. Intro (03:59)
  2. The Script Tag (04:26)
  3. OOP and DOM (02:45)
  4. What are Variables? (01:37)
  5. Strings (10:39)
  6. Creating Javascript Comments (00:52)
  7. Numbers (09:29)
  8. Boolean (09:10)

The Script Tag

Before we start writing JavaScript we need to know where in the HTML code we will place the JavaScript code. There are three places where we can insert/write our JavaScript:

  1. The head section can accommodate JavaScript as the head section of the HTML file loads before the body section. If you have a requirement where you need to run the JavaScript before the body loads, this is the only place for you: simply open and close the script tags <script> </script> and write whatever JavaScript code needed between these tags.

  2. Anywhere in the body JavaScript can be used, with the same script tags as above.

  3. A separate file. Make a new text file and save it with whatever name you want, as long as the extension of the file is “.js.” You'll write all your Javascript code in this file.You will have two files now: one HTML and one .JS (JavaScript.). Now you must return to the head section of your HTML file and put in a HTML script tag that permits us link to other files and use their content. This is the same old <script > </script>, but to tell the tag which file to link to, we include a parameter called source “src.” followed by the file name. And here it is: <script src=”home.js”></script>. We are telling the script tag that our JavaScript code is placed in a file named "home.js" (which must be located in the same folder as the HTML file, in this example) so link to that file and make the code available in the HTML file. When it loads the script tag's source parameter, it'll fetch the JavaScript code..

What's recommended?

Which way should you go? The third way is highly recommended by the coding community. First of all it will keep all your JavaScript code in one file, making it easy to manage and change. Secondly it will not increase the HTML file code length, keeping it short and clean. Most importantly, the code will be reusable. For example, if you have two files which require the same JavaScript code, these two files can now share the same JavaScript file: and if you need to make a change in the JavaScript code, you only have to change it in that one file and it will be reflected everywhere.

Got A Question?

'I love being Prime'

Have a question but don't want to ask it publicly? If you are a prime member just ask right here and we will get back to you within 48 hours. Not prime yet? no worries you can ask publicly below.

Act now while we have available seating

Our on line school is private, affordable and interactive with trainer support.
Act now save your seat before someone else takes yours.
Act now