Difference between revisions of "Embedding JavaScript into Web pages"
m (Text replacement - "<htmlet>ezoicbottom</htmlet>" to "") |
m (Text replacement - "<hr> <table border=" to "<htmlet>ezoicbottom</htmlet> <hr> <table border=") |
||
Line 64: | Line 64: | ||
+ | <htmlet>ezoicbottom</htmlet> | ||
<hr> | <hr> | ||
<table border="0" cellspacing="0"> | <table border="0" cellspacing="0"> |
Revision as of 18:14, 11 May 2016
Previous | Table of Contents | Next |
A Simple JavaScript Example | Introducing JavaScript Variables |
Purchase and download the full PDF version of this JavaScript eBook for only $8.99 |
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 (also known as inline), 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 other 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 is used to announce the version of JavaScript that is contained within the corresponding <script> elements. This argument is now deprecated.
- src- Specifies URL of an external file containing the JavaScript. 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".
As with most HTML elements the <script> body must always be terminated with the </script> element.
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:
HTML file:
<script src="/j-scripts/myjscript.js" type="text/javascript"> </script>
myjscript.js file:
document.writeln ("This is contained in an external JavaScript file")
Where to place JavaScript in a Web Page
In this section we have looked and both inline and external JavaScript (using the src argument of the <script> element). The question now arises as to which is the best approach. The general recommendation is that JavaScript functions should be placed in external src files wherever possible. The reasoning behind this is that if you have JavaScript code that is used on multiple pages you can modify this code in one place (the external JavaScript file) without having to edit every page that contains the code.
Purchase and download the full PDF version of this JavaScript eBook for only $8.99 |
Previous | Table of Contents | Next |
A Simple JavaScript Example | Introducing JavaScript Variables |