htmlScript
Client-side JavaScript code class
[Previous] [Main] [Next]


Description
htmlScript encapsulates the <SCRIPT></SCRIPT> tags. It is used to create JavaScript code that runs directly in the browser. Scripts can be located anywhere in a document, though you are generally advised to place them in the page header (using htmlPage::Head()) so that the functions and variables will be available before the document is displayed.  
 
For convenience, the htmlGroup::Add( const String& ) method, which is used by the << insertion operator, is overridden to append a carriage-return "\n" character if one is not present in the string.  
 
To make your pages viewable by the most number of browsers, use the NoScript() method to add content to be displayed by browsers that don't support JavaScript.  
 
Declaration
#include <dcmicro/htmlpp/script.h>  
 
Hierarchy
htmlscript.gif  
 
See Also
htmlNoScript, htmlApplet, htmlPlugIn  
 
Related Constants/Definitions
None.  
 
Constructors
htmlScript()  
Constructs an empty JavaScript object.  
 
htmlScript( const String& javascript_text )  
Constructs an object, inserting the string javascript_text into it.  
 
htmlScript( const htmlScript& rhs )  
Copy constructor.  
 
Destructor
virtual ~htmlScript()  
Destroys the object.  
 
Member Methods
=   htmlScript& operator= ( const htmlScript& rhs )  
Replaces the object contents with rhs, then returns a reference to the object.  
 
Add   virtual htmlObject FAR * Add( const htmlObject& element )  
Adds element unchanged to the script and returns a pointer to the new object.  
 
Add   virtual htmlObject FAR * Add( const String& text )  
Adds the string text to the script, adding a carriage-return character ("\n") if one is not present, and returning a pointer to the new object.  
 
URL   htmlScript& URL( const String& source_url )  
Defines the location of the script as source_url, causing the script to load from a separate file. A reference to the object is returned. The suffix on a location specifies the scripting language. ".js" indicates a JavaScript file. Note that the web server must be configured to associate the MIME type "application/x-javascript" with the ".js" filename suffix.  
 
URL   String URL() const  
Returns the source URL of the location of the JavaScript file, or an empty string if not set.  
 
LanguagehtmlScript& Language( const String& language )  
Defines the language used by the browser to interpret the script as language, and returns a reference to the object. Most browsers default to "JavaScript" if this attribute is not specified.  
 
LanguageString Language() const  
Returns the language setting for the script, or an empty string if not set.  
 
NoScripthtmlNoScript& NoScript()  
Returns a reference to the htmlNoScript object for the script, which can be used to add content displayed by browsers that do not support JavaScript. Browsers that do support JavaScript ignore all htmlNoScript content.  
 
Print   void Print( ostream& os ) const  
Outputs the object to os.  
 
ClonehtmlObject FAR * Clone() const  
Returns a base-class pointer to a deep copy of the object.  
 
Example Use
 
#include <stdio.h>  
#include <stdlib.h>  
#include <dcmicro/htmlpp/htmlpp.h>  
 
int main( void )  
{  
   htmlCgi   server ;  
   htmlPage  page( "html++ example application" ) ;  
 
   //  Create the script object, adding the JavaScript  
   //  code as plain text. Note how each line is added  
   //  using a separate << operator, caussing the class  
   //  to automatically append "\n" to each line.  
   htmlScript  javascript ;  
   javascript << ""  
      << "// Returns today's date as a string"  
      << "function theDate() {"  
      << "var today = new Date();"  
      << "var date  = today.getDate();"  
      << "var month = today.getMonth();"  
     << "var year  = today.getYear();"  
      << "months = new Array( 'Jan', 'Feb', 'Mar', 'Apr', 'May',"  
      << "   'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );"  
      << "    var thismonth = months[month];"  
      << "    document.write( thismonth + '. ' +  date + ', ' + (1900 + year) )"  
      << "}" ;  
 
   //  Add some content for browsers that  
   //  can't handle JavaScript  
   javascript.NoScript() << htmlItalic( "Your browser does not support JavaScript." ) ;  
 
   //  Insert the script in the header of the page  
   page.Head() << javascript ;  
 
   //  Add code to execute the script in the page body,  
   //  displaying the date in an arial font.  
   page << "Today's date is: "  
        << ( htmlFont("Arial,Helvetica") << htmlScript( "theDate();" ) )  
        << htmlBreak() ;  
 
   server << page ;  
   return 0 ;  
}  
 
Program Output
 
Content-Type: text/html  
<HTML>  
<HEAD>  
<TITLE>html++ example application</TITLE>  
<NOSCRIPT><I>Your browser does not support JavaScript.</I></NOSCRIPT>  
<SCRIPT>  
// Returns today's date as a string  
function theDate() {  
var today = new Date();  
var date = today.getDate();  
var month = today.getMonth();  
var year = today.getYear();  
months = new Array( 'Jan', 'Feb', 'Mar', 'Apr', 'May',  
'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );  
var thismonth = months[month];  
document.write( thismonth + '. ' + date + ', ' + (1900 + year) )  
}  
</SCRIPT>  
</HEAD>  
<BODY>  
Today's date is: <FONT FACE="Arial,Helvetica"><SCRIPT>theDate();</SCRIPT></FONT><BR>  
</BODY>  
</HTML>  
 
script.gif  


©1998 DC Micro Development. All rights reserved.
No portion of this document may be c opied or reproduced without expressed written consent.
html++ is a trademark of DC Micro Development.