• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 23. Importing External Content > Using an External JavaScript File

Using an External JavaScript File

You can use the <script> tag to import an external JavaScript file into an HTML document. In turn, you can use the JavaScript file to write HTML code onto the page. The advantage of using this method is that you can use the JavaScript to tailor the content as needed (see "Setting a Dynamic Header and Footer" in Chapter 22).

1.
<script src="external.js"></script>

Importing an external JavaScript file is relatively straightforward. Simply add the src attribute to the <script> tag, and don't place anything between the <script> tags (Code 23.5). This method places the external JavaScript in the HTML file at this exact location. If the JavaScript will add HTML tags to the page, those tags will be added to the page in this location.

2.
external.js

Create an external JavaScript file, and save it as external.js (Code 23.6). This file can contain any standard JavaScript, but to deliver content, you need to have the code write the HTML tags and other content. Each line of HTML code gets its own document.write. The results of importing the code are shown in Figure 23.4.

Code 23.4. The imported JavaScript writes the title and adds an image to the Web page.


Figure 23.4. The external content being imported into index.html. This content can be any standard HTML code; just don't use <html> or <body> tags.



PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint