Difference between revisions of "Embedding JavaScript into Web pages"

From Techotopia
Jump to: navigation, search
(The <script> element)
Line 14: Line 14:
  
 
* '''type''' - indicates to the browser the type of content contained within  the ''<script>'' body. This is typically be set to "text/javascript".
 
* '''type''' - indicates to the browser the type of content contained within  the ''<script>'' body. This is typically be set to "text/javascript".
 +
 +
A typical use of the ''<script>'' element when embedding the script directly into an HTML file might appear as follows:
 +
 +
<pre>
 +
<script type="text/javascript">
 +
// JavaScript code goes here
 +
</script>
 +
</pre>
 +
 +
An example of using an external file to contain the JavaScript is as follows:
 +
 +
<pre>
 +
<script src="/j-scripts/myjscript.js" type="text/javascript">
 +
</script>
 +
</pre>
 +
 +
The above example instructs the browser to load the JavaScript from the ''myjscript.js'' file located on the /j-scripts sub-directory of the local web server. It is possible to specify any valid URL here so this could be a file located on any accessible web server if required.

Revision as of 19:41, 9 April 2007

Now that we have looked at the history of JavaScript and tried some simple example JavaScript code it is time to explore the mechanisms for including JavaScript into web pages.

The <script> element

JavaScript is embedded into HTML and XHTML documents using the <script> element. This element can be used to embed the javaScript directly into the web page, or to specify an external file that contains the JavaScript.

The <script> element is used with a number of attributes:

  • defer - used to inform the browser that the script associated with this <script> element generates content (in otehr words the document.write() method is used). This can be either true or false. The default setting (i.e if this is not specified) is false.
  • language - This argument used to notify the version of JavaScript that was contained within the corresponding <script> elements. This argument is now deprecated.
  • src- Specifies URL of an external file containing the JavaScript can be found. This argument overrides any JavaScript contained within the body of this <script> element.
  • type - indicates to the browser the type of content contained within the <script> body. This is typically be set to "text/javascript".

A typical use of the <script> element when embedding the script directly into an HTML file might appear as follows:

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

An example of using an external file to contain the JavaScript is as follows:

<script src="/j-scripts/myjscript.js" type="text/javascript">
</script>

The above example instructs the browser to load the JavaScript from the myjscript.js file located on the /j-scripts sub-directory of the local web server. It is possible to specify any valid URL here so this could be a file located on any accessible web server if required.