Using URLs

As we've already seen in Chapter 1, "Understanding WAP," many standard HTML programming conventions have been translated directly to WAP. The Uniform Resource Locator (URL) is the format of the "address" a user needs to connect to and retrieve site information from the World Wide Web. It is the same standard that WML has adopted for locating resources on a network. A URL that navigates to a HTML page on the Internet looks like this:

http://www.wapforum.org/faqs/index.htm

URLs that navigate to WAP sites are structured in exactly the same way; they just send the user to a WAP site, rather than an Internet site. Because all WML browsers must conform to this standard, and implement URLs, you can use them effectively throughout your applications.

URLs have two main navigational goals in WML:

  1. Hyperlinking—allowing users to easily move between different parts of electronic documents.

  2. Locating resources external to the WML file, such as programming scripts or image files

Note

URLs can also be used to pass information in URL strings. For more information on how URLs are used to deliver data to applications, see Chapter 7, "Working with User Input."


Fortunately for those who have experience with Web development, WML uses the <a> tag (anchor tag) for linking, and the href attribute (hypertext reference attribute) to navigate to the destination.

Let's examine several examples of hyperlinking with URLs in WML.

  • An absolute URL The following tag takes us to the menu deck of BurgerWorld WAP site from any other location on the network.

    <a href="http://wap.burgerworld.com/menu.wml">
    

Tip

Use absolute URLs when you navigate between servers. For example, suppose BurgerWorld had two servers—one informational server and one e-commerce server. You would use the absolute URL to navigate between the servers. In general, using absolute URLs is a good strategy when your application changes between servers or when you are separating different components of a WML application into multiple decks.


  • A relative URL The following tag takes us to the menu deck of BurgerWorld's WAP site, but only if we are already in the same site and the same directory as the menu.wml file. From parts of the network outside of BurgerWorld's site, we will retrieve an error with this link. Relative URLs can also be expressed from the root of a machine. For example, if we were to link to menu.wml, we could express it in the form of <a href="/menu.wml">. The beginning slash in this link would signal the WAP device to work from the main directory on the WAP server the device loaded the previous deck from.

    <a href="menu.wml">
    

Tip

Always use relative URLs when possible. This makes it easier for your application to navigate from server to server. If you used only absolute URLs in your code and then moved the application's location from wap.burgerworld.com to a different server (such as wapsite1.burgerworld.com), you would have to change every link in your code. If you used relative links, a server change would require only minimal rework.


  • An absolute URL fragment WML has adopted HTML's bookmark character for naming specific locations within a single file by using the pound sign (the # character) to identify a fragment. In HTML, the fragment would locate any arbitrary place in the file, but in WML, the fragment will only work by taking you to a specific card within a deck. Those cards are identified by their card ID name. In the following card, we've set the card ID to "drinks":

    <card id="drinks" title="Drinks">
          <p>
           This is the Drink Card
         </p>
    </card>
    

    For URL fragments to work in WML, you must use card IDs; you cannot use a URL fragment to navigate to an arbitrary location within a file, like in HTML.

    In the following example we'll use an absolute URL fragment to go to the drinks card on BurgerWorld's menu.wml deck.

    <a href="http://wap.burgerworld.com/menu.wml#drinks">
    
  • A relative URL fragment If we were already at the BurgerWorld site, in the menu.wml deck, we wouldn't need to list the full URL string to navigate to the drinks page. Instead, we can use a stand-alone fragment to navigate to any card within the current deck. In the following example, we should already be in the menu.wml deck. If that's the case, the link will take us directly to the drinks card.

    <a href="#drinks">
    

Tip

The relative URL fragment is highly recommended as a navigational tool. Use this one as often as possible, as it promotes scalability, reliability, and flexibility in your applications.


Tip

A useful tag to remember when it comes to links is the <BASE> tag. The base tag allows the developer to specify an explicit URL used to resolve all external sources. This could include images and links.


Let's use an example to investigate this concept a little further so we understand exactly how links work. You'll notice that the following card is very simple. The card has two lines of text and a link to the card with a food ID. That link is relative to the same deck, and therefore you will notice there is no WML deck string specified.

<card id="main" title="Main Menu">           
     <p>
        BurgerWorld<br/>
        Main Menu<br/>
        <a href="#food">Food</a><br/>
    </p>
   </card>

The BurgerWorld menu is displayed in Figure 4.1.

Figure 4.1. A link may appear differently than regular text in WML code.


When we bring this card up on a WAP emulator, we see exactly what we expected: a card with a simple line of text and a link (see Figure 4.2). When this link is selected, the user will navigate to the card "food" within the current WML deck. This example does not show the code for the "food" card. That code is shown below.

Just for fun, let's carry this example through and look at the card food. It looks similar to main but with a few exceptions. The key exception is the link. You'll see that the link goes to home1.wml#drinks.

With this link, the user will be sent to the deck called home1.wml and to the card drinks as shown in Figure 4.3. The code for the home deck and drinks card are not shown here.

  <card id="food" title="Food">
  <p>
        BurgerWorld<br/>
        <b>Food Menu</b><br/>
                             Burgers $$2<br/>
                             Fries $$1<br/>
        <a href="home1.wml#drinks">Drinks</a><br/>
    </p>
   </card>

Figure 4.2. The user navigated to the "food" card using a relative URL fragment, and can use an absolute URL fragment to go to the "drinks" card in an entirely different deck.


If you have fully understood this section, imagine what the complete deck and its navigational structure will look like. If you can do that, you will be able to create applications with fully functional navigational schemes.

Caution

Resist the temptation of saving your cards into separate files, like you may be used to doing with HTML. Try to keep related information in a single deck, to improve download times. Three cards sent together in a single deck will take less transmission time than sending three decks which each contain a single card.


Figure 4.3. The "drinks" card must contain an absolute URL to take the user back to the home.wml deck, which is a different deck from the "drinks" card.


In our preceding example, we put more than one card into home1.wml. This deck had the card with an ID drinks point back to menu in the first deck. WAP browsers will always display the first card in the deck as a default. As a result, Because of this, the link to home1.wml#drinks in our previous example could be written in two different formats. The first format uses an absolute link fragment

<a href="home.wml#menu">Main Menu</a><br/>

But since the menu card is the first card in the home.wml deck, you don't need to write the extra URL fragment code. Instead you can simplify your code to look like this

<a href="home.wml">Main Menu</a><br/>

Either syntax will produce the same results.

Tip

It's always a good idea to specify a card fragment in a link if you know the fragment. This will ensure that your navigation scheme is still intact, even if another card is added to the top of the deck.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.218.230