1. Technology

What Does JavaScript Look Like?


A web page can consist of HTML, Cascading Style Sheets, and JavaScript. Ideally these will be split up into completely separate files but often the three are all jumbled together in the one file.One problem for those who are relatively new to creating web pages is how to work out which parts of the page content are HTML, which are CSS, and which are JavaScript. Note that we are specifically considering web pages available over the inernet here as web pages on intranets designed to run on Internet Explorer will use vbScript and JScript instead of JavaScript.

Determining what parts of your page are JavaScript is actually relatively straightforward once you know what to look for.

The first thing we need to do is to make sure we are looking at te source of the web page as displayed by a web browser. This will make sure that none of what we are looking at is code written in a server side language that is being used to generate the web page If you need to determine what's what in the original source then comparing the original file with the page the way it appears in a web browser will identify for you all those sections of the code that represent server side processing.

One thing to watch out for when viewing the source of the page in a web browser is that the source you see is the way the web browser is interpreting it. With some web browsers such as Internet Explorer the source will have been converted slightly to match the way the browser expects to process it. You may want to compare the source of a web page in several different browsers so that you can tell what may be due to changes made by a particular browser.

So having accessed the source of the web page, how do we tell what parts of the page are JavaScript?

The first thing to look for is <script> tags. Where you find a script tag that has a src attribute the value assigned to that attribute is the location of a file that contains JavaScript code.Everything in that external file will normally be JavaScript.

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

Where the script tag does not contain a src attribute you need to look at what comes between the <script> and </script> tags as what is in there will be JavaScript code.

<script type="text/javascript">
alert('this is JavaScript');

The other place that you may find JavaScript is within the HTML tags themselves. There are two ways in which JavaScript can be embedded into HTML tags.

  • While now considered to be poor practice JavaScript can be attached into HTML tags using event handlers in the HTML. Event handlers look similar to HTML attributes but can e readily identified because they all start with on for example onclick, onkeypress, onmouseover.
  • Always considered to be a bad practice, JavaScript can also be placed directly in the href attribute of an <a> tag. Where this has been done it can be easily identified by the javascript: label on the front of the JavaScript.

<span onclick="alert('this is JavaScript')">
<a href="javascript:alert('extremely bad JavaScript');">

If you examine the content of any web page looking for these parts of the web page you will be able to find all of the JavaScript that those pages contain.

Note that JavaScript does not consist entirely of the alert() statement, I have used that simply as a placeholder to represent the spot in the code where the actual JavaScript the page uses will appear.

  1. About.com
  2. Technology
  3. JavaScript
  4. Reference
  5. What Does JavaScript Look Like?

©2014 About.com. All rights reserved.