Warning: You are viewing this page with an unsupported Web browser. This Web site works best with Microsoft Internet Explorer 6.0 or later, Firefox 1.5, or Netscape Navigator 8.0 or later. Learn more about supported browsers.
Answer Box: Create a hyperlink in Publisher that opens in a new window
 by Connie Miller
Hyperlinks are handy, but sometimes you don't want people to click away from your Web site to follow them. Here's how to set up a hyperlink so that the destination opens in a new window.
| Applies to |
| Microsoft Office Publisher 2003 |
A question from your comments
I want to link to another Web page from my Web publication, but I want that page to open in a new window. How do I set that up?
Advice from the Answer Box
To create a hyperlink in Publisher that opens in a new window, you need to add an HTML code fragment to your Web publication. If you don't regularly work with HTML code, that can sound daunting. But you can find all the code that you need in this column. You just have to modify it for your situation. Note We chose the code samples specifically to solve problems that you face when you create Web sites in Publisher. They aren't suitable for all situations. If you're creating a complex Web site that you intend to update frequently or a site that includes interactive elements, consider using Microsoft Office FrontPage 2003.
What are HTML code fragments?
To add a code fragment to a Web publication, click the HTML Code Fragment command on the Insert menu. That opens the Edit HTML Code Fragment dialog box, where you can type or paste the code that you want to use.  When you close the dialog box, a code fragment object is added to your Web page. The object looks something like the following.  You can move the code fragment object around on the page the same way that you move other Publisher objects.
Which code to use
To get the destination Web page or file of a hyperlink to open in a new window, you need to add an HTML code fragment to your Web publication that contains either ECMAScript (JScript or JavaScript) or straight HTML code. Each approach has its advantages.
- ECMAScript code If you add more than one hyperlink on a page, use ECMAScript. With ECMAScript, you use the same code whether you apply the hyperlink to text or to a graphic, and you must create only one HTML code fragment object, no matter how many hyperlinks you need. You can move the HTML code fragment object out of the way because its only purpose is to contain the code. Also, when you use ECMAScript, you can control the size and appearance of the new window in which your destination opens.
- HTML code If you're even somewhat familiar with HTML code, you might be more comfortable with this approach. However, with HTML, each hyperlink that you add requires a unique HTML code fragment object. Also, you must integrate the HTML code fragment objects that you create into your Web page. When you use straight HTML code, you can't control the size or appearance of the new window in which your destination opens.
Whichever approach you choose, you don't have to write any code of your own. You can just copy the sample code provided in this column, paste it into the Edit HTML Code Fragment dialog box, and then modify the code features a bit (for example, change the URL in the sample to the URL of your destination Web page or file).
The ECMAScript code approach
Follow these steps to set up the hyperlink in your Web publication:
- In the Web publication where you want to add the hyperlink, on the Insert menu, click HTML Code Fragment.
The Edit HTML Code Fragment dialog box opens.
- Copy the following JavaScript code.
<script language="JavaScript" type="text/javascript">
<!--
function Show(Url, Name, Features)
{
window.open(Url, Name, Features);
}
// -->
</script>
- In Publisher, select the placeholder text in the Edit HTML Code Fragment dialog box, and then paste the sample code into the dialog box. Click OK.
An HTML code fragment object appears in your publication. Drag it to a location where it won't be in the way when you want to work with the text on your page. The code fragment won't show up on your published page.
- Copy the following link.
javascript:Show('http://www.fourthcoffee.com/
', 'newwindow', 'toolbar=no,scrollbars=yes,resizable=yes,height=250,width=250')
- In your publication, select the text or picture that you want to link.
- On the Insert menu, click Hyperlink.
- In the Insert Hyperlink dialog box, paste the link that you just copied into the Address box.
- The link contains three sets of single quotation marks. To customize the path for your particular destination and window, you need to replace the text between each set of quotation marks (be sure not to delete the quotation marks):
- First set of single quotation marks Replace
http://www.fourthcoffee.com/ with the URL of the Web page or the path of the file that you are linking to. Notes
- If you link to a file, the path depends on the location of the file in relation to the active page. For example, if you link from the home page to a file that is in the same directory on the Web server, the path can just be the file name (for example, Myfile.doc). If the file is in a different folder from the home page, the path must include the folder name (for example, index_files/Myfile.doc).
- If you link to another page on your Web site, use this URL:
index_files/Pagename.htm , where Pagename is the name of the page that you're linking to. When you create a Web publication, Publisher assigns default names to the pages. For example, it assigns the name index.htm to the home page and names like Page572.htm or Page319.htm to the other pages. To find the name of a page, display the page. On the File menu, click Web Page Preview. In the browser window, look at the address for the page. The page name appears after the last backslash in the path. If you want the page names to be more meaningful than the default names (for example, NewsArticle.htm instead of Page572.htm), you can assign new names to pages in your Web publication.
- Second set of single quotation marks Text between these quotation marks assigns a name to the new window that opens when you click your hyperlink. You might want to name the window if you want several hyperlinks to open in the same window, so that your customers don't have to close the window after they click each link. If you want to name the window that you're opening, leave the placeholder name (
newwindow ) where it is, or type a name that you like better. If you don't want to name the window, delete newwindow but leave the two empty single quotation marks in place.
- Third set of single quotation marks Here, you specify the features that you want the new window to have. The code in the sample specifies that the new window will:
- Have no toolbar.
- Have scrollbars.
- Be resizable.
- Be 250 pixels high and 250 pixels wide.
Replace those features with the features that you want your new window to have. The following table lists the features that you can use. Be sure to type a comma between each feature, but don't type a space.
Note If you just want the new window to open at the default restore size (not maximized) with all the regular menus and toolbars and status bar, delete all the features between the third set of single quotation marks, but leave the empty quotation marks in place.
| Attribute |
What it does |
Possible values |
| menubar |
Specifies whether to display a menu bar at the top of the window |
yes or no |
| toolbar |
Specifies whether to display the main toolbar (with the buttons for back, forward, stop, and so on) |
yes or no |
| location |
Specifies whether to display the location bar (the Address Bar in Microsoft Internet Explorer) where URLs are typed and displayed |
yes or no |
| directories |
Specifies whether to display any additional toolbars (for example, the Links bar in Internet Explorer) |
yes or no |
| status |
Specifies whether to display the status bar at the bottom of the window |
yes or no |
| scrollbars |
Specifies whether to display the horizontal and vertical scrollbars that normally appear when the page content is larger than the screen |
yes or no |
| height |
Specifies the height of the window in pixels |
A number, such as 250 |
| width |
Specifies the width of the window in pixels |
A number, such as 250 |
| left |
In Internet Explorer, specifies the distance in pixels of the new window from the left edge of the screen (for Netscape Navigator, use screenx ) |
A number, such as 0 |
| top |
In Internet Explorer, specifies the distance in pixels of the new window from the top edge of the screen (for Netscape Navigator, use screeny ) |
A number, such as 0 |
| resizable |
When enabled, allows the user to manually resize the window by dragging its edges or corners |
yes or no |
| fullscreen |
When enabled, causes the window in Internet Explorer to open in full-screen mode |
yes or no |
- When the link address is set up the way you want, click OK to close the Insert Hyperlink dialog box.
- To test your link, do one of the following:
- If the link opens a Web page, click Web Page Preview on the File menu. When the preview page opens, click the link that you created.
- If the link opens a file, you must publish your Web site to a local server. On the File menu, click Publish to the Web. If you see an information box about a Web hosting service, click OK. In the Publish to the Web dialog box, locate the local server where you want to test your Web site, and then click Save. Be sure to copy to the local server the external file that you're linking to.
Note If you have multiple hyperlinks on a page, you still need to create only one HTML code fragment. Just make sure, for each hyperlink, that the link in the Address box of the Insert Hyperlink dialog box begins with javascript:Show(
and ends with a closing parenthesis.
The HTML code approach
When you use HTML to make a hyperlink open in a new window, you must do the following:
- Create a different code fragment for each hyperlink that you add.
- Integrate the HTML code fragment objects that you create into the content of your Web page.
When using HTML, you don't use the Insert Hyperlink dialog box because the link information is included in the HTML code fragment.
To apply the hyperlink to text
- Copy the following code.
<p style="font-family:arial;font-size:80%">I want the destination of this link to open in a
<a href="http://www.fourthcoffee.com/" target="_blank">new window</a>, not the same window.</p>
- In Publisher, display the Web page where you want to add the link.
- On the Insert menu, click HTML Code Fragment.
The Edit HTML Code Fragment dialog box opens.
- Select the placeholder text in the dialog box, and then paste the code that you copied into the dialog box. Now, modify the code features a bit to fit your particular situation.
- In the
<a href="http://www.fourthcoffee.com/" target="_blank"> section of the code statement, replace the URL between the quotation marks (http://www.fourthcoffee.com/ ) with the URL or the path of the file that you want to link to. Be careful not to delete the quotation marks. Note If you link to a file, the path depends on the location of that file in relation to the current page. For example, if you link from the home page to a file that is in the same directory on the Web server, the path can be the file name (for example, Myfile.doc). If the file is in a different folder from the home page, the path must include the folder name (for example, index_files/Myfile.doc).
- The
<p style="font-family:arial;font-size:80%"> section of the code statement sets the font for the text in the HTML code fragment so that it matches the rest of the text on your page. Replace arial with the font that is applied to the text on the page. You may have to experiment a bit with font size. For regular body text, 70% or 80% will probably be right. To see the text size of the code fragment, click OK to close the Edit HTML Code Fragment dialog box, and then click Web Page Preview on the File menu.
- In the sample code,
new window is the text that is linked. Replace new window with the text that you want the hyperlink applied to on your Web page.
- The text that precedes
<a and follows </a> appears on your page in the same sentence as the hyperlinked text. Replace this sample text with your own text. Be careful to include a space before the <a and after the </a> . If the hyperlinked text stands alone and is not part of a sentence, just delete the text that precedes <a and follows </a> .
- When the code in the Edit HTML Code Fragment dialog box looks the way you want, click OK.
An HTML code fragment object appears in your publication. Drag it to the location on your page where you want the hyperlinked text to appear. You may have to experiment a bit to position the code fragment object so that the link appears exactly where you want it.
- To test your link, do one of the following:
- If the link opens a Web page, click Web Page Preview on the File menu. When the preview page opens, click the link that you created.
- If the link opens a file, you must publish your Web site to a local server. On the File menu, click Publish to the Web. If you see an information box about a Web hosting service, click OK. In the Publish to the Web dialog box, locate the local Web server where you want to test your Web site, and then click Save. Be sure to copy to the local server the external file that you're linking to.
To apply the hyperlink to a picture
- Copy the following code.
<a href="http://www.fourthcoffee.com/" target="_blank">
<img src="Picture.gif" alt="Explanatory note"></a>
- In Publisher, display the Web page where you want to add the link.
- On the Insert menu, click HTML Code Fragment.
The Edit HTML Code Fragment dialog box opens.
- Select the placeholder text in the dialog box, and then paste the code that you copied into the dialog box. Now modify the code features to fit your particular situation.
- In the
<a href="http://www.fourthcoffee.com/" target="_blank"> section of the code statement, replace the URL between the quotation marks (http://www.fourthcoffee.com/ ) with the URL or the path of the file that you want to link to. Be careful not to delete the quotation marks. Note If you link to a file, the path depends on the location of that file in relation to the active page. For example, if you link from the home page to a file that is in the same directory on the Web server, the path can be the file name (for example, Myfile.doc). If the file is in a different folder from the home page, the path must include the folder name (for example, index_files/Myfile.doc).
- In the
<img src="Picture.gif" alt="Explanatory note"> section of the code, replace the picture file name between the quotation marks (Picture.gif ) with the file name of the picture that you want to link. When you publish your Web site, make sure that you also upload the picture file. If you intend to put the picture file into a folder on the Web server, you must include the path as well as the file name in the code. For example, if you put the picture in a folder called index_files, the code looks like the following.
<img src="index_files/Picture.gif">
Also, replace Explanatory note with alternative text that appears if the browser cannot display your picture.
- When the code in the Edit HTML Code Fragment dialog box looks the way you want, click OK.
An HTML code fragment object appears in your publication. Drag it to the location on your page where you want the hyperlinked picture to appear. You might have to experiment a bit to position the code fragment object so that the picture appears exactly where you want it.
- To test your link, it's best to publish your Web site to a local Web server. On the File menu, click Publish to the Web. If you see an information box about a Web hosting service, click OK. In the Publish to the Web dialog box, locate the local server where you want to test your Web site, and then click Save. Be sure to copy the picture file (for example, Picture.gif) to the appropriate location on the local server. And, if you're linking to an external file, copy that file to the local server.
About the author
Connie Miller, a writer on the Microsoft Office User Assistance team, collects and responds to questions and suggestions that you submit to the Publisher Answer Box. To submit a detailed question of your own, click Feedback.
|