Other Uses for Hyperlinks


In addition to being able to pass information around, we can implement other functionality using hyperlinks. While the href attribute was initially designed for linking to other files, it can also be used to send e-mails, call client-side scripts (such as JavaScript or VBScript), and link to much more than just web-based resources. In this section, we'll take a look at the first two of these situations – the most common alternative uses for hyperlinks – and then take a quick look at the other options open to us.

E-Mailing

One of the most common uses for Anchor tags, after linking files to one another, is to initiate the sending of an e-mail. This is regularly included on a website whenever feedback or comments are wanted, as it saves the user from having to type in, or copy and paste the e-mail address from the page, and also removes the possibility of the user entering the address incorrectly.

Try It Out—E-mailing the Band

In this example, we'll add a Contact Us page to the website, providing different means of getting in touch with the band. One of these options will be a hyperlink, which, when clicked, will bring up a window with which you can compose mail (depending on your system setup, this could be within Outlook, Outlook Express, or any other mail reader such as Pegasus).

  1. Create a new ASP.NET Page called ContactUs.aspx, and on the design canvas, enter text to make the page look like the one shown below, using a Heading 1 for the title, and the Bold button on the toolbar to highlight the methods of contact (to get the line breaks in the address, instead of paragraph marks that incorporate additional vertical spacing, press Shift+Enter, rather than just Enter):

  2. Beneath the address, enter a section marked E-mail:, and follow it with a value of cam@tempuri.org. When you move your mouse cursor away from this line once it's complete, you'll see that Web Matrix has automatically converted this into a hyperlink. If you click on the link to select it, and look at the Properties panel, the href attribute will have been set to mailto:cam@tempuri.org. This is the method by which we can begin e-mails from links.

  3. We can add to the href value so that it opens up the e-mail with certain fields filled in for us. To do this, amend the value so that it looks like the following:

    mailto:cam@tempuri.org?Subject=Website%20Feedback& Body=Your%20site%20rocks!

    You will find that once this value has been entered, the text on the canvas is also updated – this is due to Web Matrix linking the text that appears, and that href attribute. To correct this, switch to HTML view, locate the link, and amend the text between the <a> and </a> tags so that it says cam@tempuri.org once again.

  4. Save this page, and click the Start button on the toolbar to show the web page in Internet Explorer. When the page is shown, click on the cam@tempuri.org link at the bottom of the page. A new e-mail message should be opened, with the To, Subject, and Body already containing values, as shown below:

    click to expand

How It Works

Whenever a hyperlink is clicked on, the browser first interprets the contents of the href attribute, rather than just blindly navigating to a URL specified within it. If the value of the attribute begins with one of a predefined list of items, including mailto:, then a special action is taken. In this case, the browser tries to open a new e-mail. The program that is used to do this depends on the web-browser itself. In Internet Explorer, this can be changed by selecting the Tools | Internet Options menu entries, then altering the E-mail option on the Programs tab.

The rules for initiating an e-mail from within a link are very similar to those for passing data between pages – the first parameter is prefixed by a question mark (?) character, and any further ones are prefixed (separated) by ampersand (&) characters. The following parameters can used in a mailto:

  • Subject – Text to appear in the subject line of the message

  • Body – Text to appear in the body of the message

  • CC – Addresses to be included in the "cc" (carbon copy) section of the message

  • BCC – Addresses to be included in the "bcc" (blind carbon copy) section of the message

Any of these parameters that are specified must follow the same rules as for normal (HTTP) query string parameters that we've already looked at. For instance, a less-than symbol (<) must be encoded as %3C, a greater-than symbol (>) as %3E, and so on.

JavaScript

Another of the common uses for hyperlinks is the calling of client-side JavaScript (or VBScript) code. This is a very useful feature, as it allows for far more complex actions to be carried out than would otherwise be possible – such as the validating of fields that have been completed, the initiation of multiple actions at the same time, and so on. Calling JavaScript from a hyperlink can be done in a similar manner to sending an e-mail, as we'll see below.

Try It Out—Using JavaScript in a Link

In this example, we'll update the pop-up window that we added to the homepage of the Cornflakes at Midnight site. We'll use JavaScript to open it in a fixed-sized window without menus, and so on, rather than the standard browser window it currently opens in.

  1. Within Web Matrix, open the Default.aspx file and switch to Code view so that we can edit the links that are created based upon the time of day.

  2. Amend the code within the Page_Load subroutine so that it matches the following:

    If Now.Hour<12 Then   lnkTimeOfDay.Text = "Cornflakes before lunch"  lnkTimeOfDay.NavigateURL = "javascript:window.open(" & _  "'PhotoAM.aspx', 'Photo', 'width=400,height=300');" Else   lnkTimeOfDay.Text = "Cornflakes after lunch"  lnkTimeOfDay.NavigateURL = "javascript:window.open(" & _  "'PhotoPM.aspx', 'Photo', 'width=400,height=300');" End If
  3. Save the file, and then click the Start button on the toolbar to display the page. When the page is displayed, click on the Cornflakes before lunch, or Cornflakes after lunch link, depending on the time of day. When you do this, you should see the photo open up in a new window, with the toolbars, menus, and so on:

    click to expand

  4. This will also update the original window as well, however, showing the value returned from the window.open call. To fix this, we can write our own JavaScript function to open a window that does not return a value. To do this, update each reference to window.open in the Code view to openWindow. Then switch to HTML view so that we can write our JavaScript function. Between the <head> and </head> tags at the top of the page, enter the following:

    <html> <head>  <script language="javaScript">  function openWindow(url, name, parameters) {  window.open(url, name, parameters);  }  </script> </head> <body>    ...

    If this page is saved and viewed again, when the link is clicked, the original page
    will remain.

How It Works

Just as with our mailto: example, if we prefix some code in an href attribute with javascript:, the browser detects this and knows to execute the instructions, rather than trying to navigate to a URL. The main differences between the two are that the javascript: protocol does not need to follow the same conventions of separating items with ? and & characters, and the result of the code is returned and processed.

As with many JavaScript functions, window.open returns a value. In this example, a reference to the window that was opened is returned. It is very rare that we want to display this value in the window, so we must find a way of performing the action without getting a return value. The simplest way of doing this is writing a JavaScript function that makes the call for us, but ignores the result, and does not return anything. Other options include settings two attributes on the anchor – an href of javascript:void, and an onclick of the code that is to be executed. The problem with this is that the client-side onclick event (and associated attribute) is not supported by the ASP.NET Hyperlink control.

Important

When entering JavaScript statements in a hyperlink, be sure to use different quotation marks for delimiting the link from entering strings within the script. For instance <a href=”javascript:alert(‘ding’),”/>is valid, but <a href=”javascript;alert(“ding”),”/>is not.

Other Protocols

You may have noticed that there are some commonalities between the different functions we've used hyperlinks for, whether sending an e-mail or opening a web page; all the href links follow a similar pattern: protocol:data. For instance:

http://www.wrox.com mailto:someone@tempuri.org javascript:alert('hello');

This pattern continues with several other protocols that can be accessed from hyperlinks – most commonly for accessing different types of remote resources, rather than the special case of JavaScript. The majority of these follow the http:// style, and include the following:

Protocol

Example

Description

HTTPS

https://www.microsoft.com

The secure hypertext transfer protocol (HTTPS) is a communications protocol that allows information to be sent between computers on the Web in an encrypted format. HTTPS is HTTP using a Secure Socket Layer (SSL). A secure socket layer is an encryption protocol invoked on a web server that uses HTTPS.

The HTTPS protocol is usually used for online purchasing, or the exchange of private information.

FTP

ftp://lcweb.loc.gov/

The File Transfer Protocol (FTP) allows files to be transmitted across the Web. It is more appropriate for large files than HTTP, as it allows transfers to be resumed partway through, should they fail, among other features.

Explorer has a built-in FTP client that makes the transfer of files over the Internet as simple as between local folders.

Telnet

telnet://locis.loc.gov/

The TELNET protocol allows text-based programs to be run on remote computers, with your own computer acting as a terminal, taking keyboard input, and showing the results of any processing, and so on.

Windows comes with a Telnet client built into it that is launched whenever a telnet address is entered into Internet Explorer.

Gopher

gopher://gopher.loc.gov/

Gopher, made available in 1991, became an extremely popular service because it gave access to information and services on the Internet through a relatively easy-to-use menu system.

A third-party program is required to use the gopher protocol on Windows.

Many of these protocols (Telnet, Gopher, and so on) are slowly dying out, with few new systems being developed for them. For this reason, and because of the large differences between them and web sites, we'll not cover them further in this book. It is worth remembering that hyperlinks aren't necessarily used for simply linking to resources that are related to websites (such as JavaScript, and other web pages), though.




Beginning Dynamic Websites with ASP. NET Web Matrix
Beginning Dynamic Websites: with ASP.NET Web Matrix (Programmer to Programmer)
ISBN: 0764543741
EAN: 2147483647
Year: 2003
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net