Links to Places Within Other Documents
[Previous] [Main] [Next]

 
In order to create a link to a specific place within another document, the place within the destination document must be given a label. A label identifies a place within a document that can be accessed from another hyperlink, and is created using the htmlLabel class. htmlLabel encapsulates the HTML <A NAME></A> tags, and inherits from htmlGroup so it can contain other html++ objects and text.  
 
Let's say you have a list of frequently asked questions in one document, and the corresponding answers in another document. You might choose to identify each question/answer pair by a unique text name, or you might choose instead to number them. Within the answer document, all you need to do is insert an htmlLabel object before the answer. For example:  
 
#include <stdio.h>  
#include <stdlib.h>  
#include <dcmicro/htmlpp/htmlpp.h>  
 
int main( void )  
{  
   htmlCgi   server ;  
   htmlPage  page( "answers" ) ;  
 
   page << htmlLabel( "A1" )  
        << "This is the answer to the first question."  
        << htmlParagraph() ;  
 
   page << htmlLabel( "A2" )  
        << "This is the answer to the second question."  
        << htmlParagraph() ;  
 
   server << page ;  
   return 0 ;  
}  
 
When executed, the above example produces the following HTML output:  
 
Content-Type: text/html  
<HTML>  
<HEAD>  
<TITLE>answers</TITLE>  
</HEAD>  
<BODY>  
<A NAME="A1"></A>This is the answer to the first question.<P></P>  
<A NAME="A2"></A>This is the answer to the second question.<P></P>  
</BODY>  
</HTML>  
 
If the "answers" page were not generated using html++, you would manually insert the HTML code for the label as follows:  
 
     <A NAME="A1"></A>  
 
The "questions" document would have a list of questions as hyperlinks to the corresponding labels within the "answers" document. The following program demonstrates one way to construct such a document using htmlHyperLink objects:  
 
#include <stdio.h>  
#include <stdlib.h>  
#include <dcmicro/htmlpp/htmlpp.h>  
 
int main( void )  
{  
   htmlCgi   server ;  
   htmlPage  page( "questions" ) ;  
 
   //  This example assumes that the answers  
   //  document is stored as "answers.html"  
   page << htmlHyperLink( "/answers.html",   
                          "A1",  
                          "Click here for answer 1" ) ;  
 
   //  Skip a line  
   page << htmlParagraph() ;  
 
   //  Create another hyperlink, but use a different  
   //  constructor and method to achieve the same result.  
   htmlHyperLink  q2( "/answers.html" ) ;  
   q2.Label( "A2" ) ;  
   q2 << "Click here for answer 2" ;  
   page << q2 ;  
 
   server << page ;  
   return 0 ;  
}  
 
 
When executed, the above program produces the following HTML output:  
 
Content-Type: text/html  
<HTML>  
<HEAD>  
<TITLE>questions</TITLE>  
</HEAD>  
<BODY>  
<A HREF="/answers.html#A1">Click here for answer 1</A><P></P>  
<A HREF="/answers.html#A2">Click here for answer 2</A>  
</BODY>  
</HTML>  
 

ex_hyperlink1.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.