Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

4. Getting Connected: A Trip to Webville > Opening a new window using target

Opening a new window using target

To open a page in a new window, you need to tell the browser the name of the window in which to open it. If you don’t tell the browser a specific window to use, the browser just opens the page in the same window. You can tell the browser to use a different window by adding a target attribute to the <a> element. The value of the target attribute tells the browser the “target window” for the page. If you use “_blank” for the target, the browser will always open a new window to display the page. Let’s take a closer look:

image with no caption

Exercise

Open your Starbuzz “index.html” file. Add the target attribute to the <a> tag that links to the Caffeine Buzz page. Now give it a try – did you get a new window?

Brain Power

Can you think of some advantages and some disadvantages to using the target attribute to open a page in a new window?

There are no Dumb Questions

Q:

Q: What if I have more than one <a> element with a target? If there’s already a “_blank” new window open, will it open in the window that’s already open? Or will it open in a new “_blank” window?

A:

A: If you give the name “_blank” to the targets in all your <a> elements, then each link will open in a new blank window. However, this is a good question because it brings up an important point: you don’t actually have to name your target “_blank”. If you give it another name, say, “coffee”, then all links with the target name “coffee” will open in the same window. The reason is that when you give your target a specific name, like “coffee”, you are really naming the new window that will be used to display the page at the link. “_blank” is a special case that tells the browser to always use a new window.

The Target Attribute Exposed

This week’s interview: Using target considered bad?

Head First:  Hello target, we’re so glad you could join us.

Target Attribute:  I’m glad to be here. It’s nice to know you’re still interested in hearing about me.

Head First:  Why do you say that?

Target:  Well, to be honest, I’m not as popular as I used to be.

Head First:  Why do you think that is?

Target:  I think it’s because users want to be in control of when a window opens. They don’t always like new windows popping open at unexpected times.

Head First:  Well, it can be very confusing – we’ve had complaints from people who end up with so many windows on their screens, they can’t find the original page.

Target:  But it’s not like it’s difficult to get rid of the windows... just click on the little close button. What’s so hard about that?!

Head First:  True, but if users don’t know a new window has opened then they can get confused. Sometimes the new window completely covers the old window and it’s hard to tell what’s happening. That can be confusing for anyone, but especially for someone with a visual impairment.

Target:  Oh, I never thought of that.

Head First:  Well, think about it: if someone’s got their browser window magnified, and a whole new window opens on top of the one they’re reading, it can be very confusing for them. It’s hard to tell what’s going on when you’re not looking at the whole screen at once.

Target:  Yeah I suppose it would be. It’s probably difficult for using screen readers, too.

Head First:  Yup. Some screen readers play a sound when a new window opens, but others just ignore the new window completely, or else they jump right to the new window immediately. Either way, it’s gotta be confusing for someone who can’t see what’s going on. And of course, since the page is in a whole new window, there’s no way to use the back button to get back to the original window.

Target:  [Sigh] I’m starting to see why I’m not as popular as I used to be.

Head First:  Don’t get too depressed; there are times when it’s nice to have a new window open, right?

Target:  Yes, I always thought having those little informational windows for “extra information” was handy, and I’m especially proud when people use me to open large versions of images. That way, the user can view the large image and then go right back to the main page.

Head First:  Okay, see, you do come in handy at times. We’ve just got to remember to use you when it’s appropriate, but to keep in mind those people who might be visually impaired and not overuse you.

Target:  Right!

HTMLcross

Here are some mind benders for your left brain.

image with no caption

Across

Down

2. Wrong way to pronounce URL.

4. Attribute used to anchor an <a> element to a page.

7. Earl sold these.

8. Web address to a resource.

9. Protocol we’ve been using up until this chapter.

11. Unique name on the Web.

12. Always use these kinds of links when linking to pages on the same server.

13. Request/response protocol.

14. Most popular Mac FTP application.

1. People scan these rather than reading text.

3. Path from the root.

5. What are you supposed to send back from Webville?

6. Informative caffeine site.

7. Keep your link labels ______.

10. Controls domain names.

11. _______ file you get when you ask for a directory.

12. Top directory of your Web site.

Bullet Points

  • Typically the best way to get on the Web is to find a hosting company to host your Web pages.

  • A domain name is a unique name, like amazon.com or starbuzzcoffee.com, that is used to identify a site.

  • A hosting company can create one or more Web servers in your domain. Servers are often named “www”.

  • The File Transfer Protocol (FTP) is a common means of transferring your Web pages and content to a server.

  • FTP applications, like Fetch for Mac or WS_FTP for Windows, can make using FTP easier by providing a graphical user interface.

  • A URL is a Uniform Resource Locator, or Web address, that can be used to identify any resource on the Web.

  • A typical URL consists of a protocol, a Web site name, and an absolute path to the resource.

  • HTTP is a request and response protocol used to transfer Web pages between a Web server and your browser.

  • The file protocol is used by the browser to read pages from your computer.

  • An absolute path is the path from the root folder to a file.

  • “index.html” and “default.htm” are examples of default pages. If you specify a directory without a filename, the Web server will look for a default page to return to the browser.

  • You can use relative paths or URLs in your <a> element’s href attribute to link to other Web pages. For other pages in your site, it’s best to use relative paths, and use URLs for external links.

  • Use the id attribute to create a destination anchor in a page. Use ‘#’ followed by a destination anchor id to link to that location in a page.

  • To help accessibility, use the title attribute to provide a description of the link in <a> elements.

  • Use the target attribute to open a link in another browser window. Don’t forget that the target attribute can be problematic for users on a variety of devices and alternative browsers.

image with no caption

Sharpen your pencil: Solution

image with no caption
image with no caption

Earl needs a little help with his URLs: Solution

image with no caption

Exercise Solutions

Add a title to the link to “mission.html” with the text “Read more about Starbuzz Coffee’s important mission”. Notice that we didn’t make the mission link’s label as concise as it should be. Shorten the link label to “our Mission”. Here’s the solution; did you test your changes?

image with no caption