javascript for journalists

Where does JavaScript reside?

JavaScript is stored right inside the HTML of a webpage. The code itself is put in one of two places:

Linking to a JavaScript file

JavaScript can be stored in a separate text file that is referenced in your HTML. Generally, this separate text document has the file extension .js

The link to this JavaScript document can be placed anywhere in your HTML, but typically it appears in the <head> of your document so that it's loaded quickly, and before the rest of your webpage. There are times when you want to load your JavaScript last, before the closing </body> tag. This helps to speed up the loading of your webpage, since the browser processes the code last. This is often done when the JavaScript code is not critical to the timing of the loading of the page.

JavaScript is included on a webpage using the <script> tag. To link to a javascript file, use the following syntax:

<script type="text/javascript" src="path_to_file/filename.js"></script>

Javascript screenshot

It is important to note that when linking to JavaScript files, you need both an opening and closing <script> tag. 

Inline JavaScript

JavaScript can also be written directly within the HTML of a webpage, also using the <script> tag. Put your code between the opening and closing script tags. However, you should omit the src="" attribute from the "Linking to a JavaScript File" example above. 

<script type="text/javascript">
	//JavaScript code here

It's important to note that <script> tags can be used for more than just JavaScript code. So while the attribute type="text/javascript" is not required, but is good practice to use. There is a lot of debate on whether to use it, since some older browsers might intrepet differently. A plain <script> tag will also work, as all modern browsers automatically default to JavaScript language when the see a <script> tag with no attribute.

Noscript tags

While it's not required, there is a special <noscript> tag in HTML which allows you to display content to users who use browsers that don't support JavaScript, or who have JavaScript turned off. In recent years, there is less concern that browsers may not support JavaScript since virtually every browser today uses JavaScript (even mobile devices). This noscript feature is most often used for users of screen readers, like the blind, who browse the web with special software or devices that speak out the content of webpages. Noscript tags are a method of displaying some safety text, especially in situations when you are using JavaScript to manipulate the content of the page—which doesn't happen when JavaScript is disabled.

<script type="text/javascript">
	document.write("Text that won't show up if a user has JavaScript 
	disabled in their browser.");

<noscript>Text that will show up if, and only if, a user has JavaScript 
disabled in their browser.</noscript>

Remember, the contents of a noscript tag won't display for most users. Only those who are using a browser that has JavaScript disabled.