As covered in the tutorial on Running a Script, this involves removing a script from an HTML page and placing it in a separate JavaScript file then referring to it from within the HTML page.
This allows you to reuse code required by multiple pages - simplifying maintenance. An include file that has been downloaded is held in cache and doesn't need to be downloaded again.
The test.js Include File used by the Example above
document.write('Hello World');
Problems with HTML Validation
Sometimes when you use the document.write() method to output HTML in a page you may encounter a problem validating the page.
This is because the validator reads the HTML tags within the JavaScript statement and assumes them to be part of the HTML code.
If you've used a decision branch to output a choice of start tags, with a single end tag after the branch, then a mismatch of start and end tags will be assumed by the validator.
To avoid this remove the JavaScript to an include file and the validator will ignore it.
Problems with HTML Validation with Flash
If you embed a Flash movie within a page you may encounter a problem with validation. This problem isn't related to JavaScript but can be resolved with it.
The problem occurs because of the different ways that different browsers display Flash.
Internet Explorer browsers use the HTML <object> tag, which is recognised by the W3C validator, and Mozilla browsers use the <embed> tag, which is not recognised by the W3C validator.
However, for the Flash movie to display in both Internet Explorer and Mozilla, both tags must be used.
To get round the problem a JavaScript include file may be used to hold all the required HTML code within a document.write() statement.
All carriage return and tab characters should be removed first or the JavaScript will produce an error.