Uniform Resource Locators (URLs)

Each web page on the Internet needs some sort of unique name so it can be identified. A page can be accessed by a Uniform Resource Locator (URL). URLs can be used to identify HTML files, PHP files, image files, plain text files, and many other file types. Here is an example URL:

Using the "target" Attribute to Launch a New Browser Window
When a user clicks on an external link from your website, they will normally leave your web site as the browser follows that external link. However, you may want them to see the information on the new website without leaving your site. This way, once they are done with the new site, they can easily move back to the content on your page. To do this, you need a way to send them to an external link in a new browser window and keep your site open in the current browser window.

You can create a new browser window or tab by putting a target attribute inside the anchor tag:The target value is the name of the browser window where you want the new site to appear. If there is no target attribute, the external site will appear in the current browser window, replacing your site. However, you can specify a target to open the new site in a new window.

The target value always starts with an underscore (_), followed by a name. Any links going to a named target will appear in the same browser window. Consider the example below, where two links have a "_cow" target and three links have a "_pig" target:The "target" attribute works on internal anchor links too! Even when staying within your own site, you may want to launch some special content such as a product description or picture in a new browser window.
You can force a brand new browser window to open on every click by using the target name "_blank". This will open a new unnamed window each click, no matter how many times a link with a "_blank" target name is clicked. This approach may be a better option if you want each page to appear in a new window.

Experiment with the sample external hyperlink below. The target is initially "_blank", so each time you click on the link in the output panel, your web browser should launch a new window or tab.Now, change the target to some other value that you define and refresh the output panel. Each time you click on that new link, it will direct your browser to the same new tab or window instead of creating a new window on each click.

1. What is a URL? Describe and diagram the different parts of a common URL.
2. Demonstrate the difference between the href attributes of an internal and an external link.
3. What is a trailing slash used for in a URL? When would you use the trailing slash and when would you not?
4. What does the target attribute do in an anchor element?

1. A URL (Uniform Resource Locator) is a unique address that identifies a webpage or a resource on the internet. It is used to access and locate specific files or webpages.

A common URL consists of different parts:

- Protocol: This is the first part of the URL and represents the transfer protocol used to access the resource. It is usually "http://" or "https://" for webpages.
- Domain: This is the main part of the URL and represents the address of the website or server hosting the resource. It is typically a registered domain name.
- Path: This part specifies the directory or location of the specific resource within the domain. It can include subdirectories and file names.
- Query Parameters: These are optional parameters that can be added to the URL to pass information to the server. They are separated from the path by a question mark (?) and can have multiple key-value pairs.
- Fragment Identifier: Also known as the anchor, it is used to link to a specific section or location within a webpage. It is indicated by a hash symbol (#) followed by the identifier.

Here is a diagram illustrating the different parts of a common URL:

```
+------------------------------------------------------------+
| URL |
+------------------------------------------------------------+
| Protocol | Domain | Path |
+----------------+-----------------------------+-------------+
| http:// | example.com | /blog/ |
+------------------------------------------------------------+

```

2. The `href` attribute of an anchor tag (`<a>`) is used to specify the destination or URL of the link.

- For an internal link, the `href` attribute would typically be set to the relative path of the webpage or resource within the same website. For example, `<a href="/about">About</a>` would link to the "about" page within the same website.
- For an external link, the `href` attribute would include the full URL (including the protocol and domain) of the external webpage or resource. For example, `<a href="https://www.example.com/">Example Website</a>` would link to the "https://www.example.com/" page.

3. A trailing slash (/) in a URL is used to indicate a directory or a folder-like structure. It is usually added at the end of the URL, after the domain.

- The trailing slash is used when accessing a directory or location with multiple resources or files within it. For example, "https://www.example.com/blog/" would typically indicate a blog directory with multiple blog posts inside it.
- The trailing slash is not used for specific files or resources. For example, "https://www.example.com/blog/post1" would typically represent a specific blog post file.

Whether to use a trailing slash or not depends on the server configuration and URL structure. It is important to be consistent to avoid potential duplicate content or broken links.

4. The `target` attribute in an anchor (`<a>`) element is used to specify where the linked content should be opened when clicked. It determines the browser window or tab behavior.

- If the `target` attribute is not specified, the linked content will open in the same browser window or tab, replacing the current page.
- If the `target` attribute is set to `_blank`, the linked content will open in a new, unnamed browser window or tab.
- The `target` attribute can also be set to a specific name (_top, _parent, etc.) to control how the linked content opens in relation to frames or iframes.

Using the `target` attribute allows website owners to control the browsing experience and keep their own website open while opening external links in separate windows or tabs.

1. A URL (Uniform Resource Locator) is a unique address that identifies a specific resource on the internet. It consists of several parts:

- Protocol: The protocol indicates the type of connection being used to access the resource. Examples include http://, https://, ftp://, etc.

- Domain: The domain is the registered name of the website or server hosting the resource, such as google.com or example.com.

- Subdomain: The subdomain is an optional hierarchical part of the domain. It precedes the main domain and is separated by a dot, e.g., www.example.com.

- Directory: The directory refers to the specific location or path on the server where the resource is located. It represents the folder structure on the server.

- File Name: The file name is the name of the specific resource being accessed, such as index.html, image.jpg, etc.

- Query Parameters: Query parameters provide additional information or options to the resource being accessed. They are usually specified after a question mark (?) and separated by ampersands (&).

- Fragment Identifier: The fragment identifier refers to a specific section or anchor within the resource that should be displayed when the page is loaded or scrolled to. It is indicated by a hash symbol (#) followed by the identifier.

Here is a diagram showing the different parts of a common URL:
```
|-----------------------------------------------|
| |-------------------| |
| Protocol | Domain | Fragment |
| |-------------------| Identifier |
|-----------------------------------------------|
| |-----------------| |
| Subdomain| Directory | |
| |-----------------| |
|-----------------------------------------------|
| |--------------| |
| | File Name | Query Parameters |
| |--------------| |
|-----------------------------------------------|
```

2. The href attribute in an anchor element specifies the destination URL when the link is clicked. The difference between an internal and external link lies in the value of the href attribute.

- Internal Link: An internal link points to a resource within the same website or domain. The href attribute value would typically contain a relative path, such as "/about" or "contact.html", indicating the location of the resource relative to the current page.

- External Link: An external link points to a resource on a different website or domain. The href attribute value would include the full URL, starting with the protocol, domain, and possibly the directory and file name.

Example:
```
Internal Link: <a href="/about">About</a>
External Link: <a href="https://www.example.com">Example</a>
```

3. A trailing slash ("/") in a URL is used to indicate that the resource being accessed is a directory or folder, rather than a specific file. It is generally considered a good practice to include a trailing slash for consistency and clarity, especially for directory URLs.

- Use Trailing Slash: When accessing a directory or listing of files in that directory, it is recommended to include a trailing slash. For example, "https://www.example.com/directory/".

- Omit Trailing Slash: When accessing a specific file within a directory or when the resource being accessed is not a directory, the trailing slash is typically omitted. For example, "https://www.example.com/directory/file.html".

Including or omitting the trailing slash can affect how the server handles the request, and it may impact SEO and URL structure.

4. The target attribute in an anchor element specifies where the linked resource should open when clicked. It determines the behavior of the browser when navigating to the linked page.

- "_self": The linked resource opens in the same browser window/tab, replacing the current page.

- "_blank": The linked resource opens in a new, unnamed browser window/tab.

- "_parent": The linked resource opens in the parent frame if the current window contains frames or iframes.

- "_top": The linked resource opens in the full body of the window, replacing any frames or iframes.

- Custom Name: The target attribute can also specify a custom name for a named target window or frame. This allows multiple links to open in the same named window or frame.

Example:
```
<a href="https://www.example.com" target="_blank">Open in New Window</a>
<a href="https://www.example.com" target="_self">Open in Same Window</a>
```

1. A URL (Uniform Resource Locator) is a string of characters that provides a unique address for a resource on the internet. It is used to locate and retrieve resources such as web pages, images, videos, and files.

Here is a diagram of the different parts of a common URL:

```
scheme host path query fragment
| | | | |
| | | | |
┌────┴─────┐ ┌─────┴────┐ ┌────────────┴────┐ ┌┴───┐ ┌─────┴────┐
https://www.example.com/page.html?type=article#section-1
```

- Scheme: It specifies the protocol for the URL, such as HTTP or HTTPS.
- Host: It identifies the domain or IP address where the resource is located.
- Path: It indicates the specific location or directory path of the resource on the server.
- Query: It provides additional parameters or data that can be passed to the server.
- Fragment: It refers to a specific section within the resource, typically used for web page anchors or sections.

2. The `href` attribute of an anchor element (`<a>`) is used to specify the destination of a link. The difference between the `href` attributes of an internal and an external link is the URL itself.

- Internal Link: An internal link refers to a link within the same website or domain. The `href` attribute of an internal link typically contains a relative URL that points to another page or resource within the same website. For example: `<a href="/about">About</a>` points to the `/about` page within the same website.

- External Link: An external link refers to a link that points to a different website or domain. The `href` attribute of an external link contains an absolute URL that includes the full address of the linked resource. For example: `<a href="https://www.example.com">Example Website</a>` points to a website outside of the current website.

3. A trailing slash in a URL is used to indicate a directory or folder on a web server. It typically appears at the end of the URL path.

- Use of Trailing Slash: The trailing slash is used when the URL represents a directory or folder on a web server. For example, `https://www.example.com/blog/` refers to the "blog" directory on the server. This is useful when there are multiple resources within that directory, such as different blog posts.

- Omission of Trailing Slash: The omission of a trailing slash implies that the URL is pointing directly to a specific resource (file) rather than a directory. For example, `https://www.example.com/page.html` refers to a specific HTML file. This is common for individual pages or files rather than directories.

4. The `target` attribute in an anchor element (`<a>`) specifies how the linked resource should be displayed when clicked.

- `_blank`: When the `target` attribute is set to `"_blank"`, it opens the linked resource in a new browser window or tab. This allows the user to view the linked content without navigating away from the current page. For example, `<a href="https://www.example.com" target="_blank">Example Website</a>` opens the linked website in a new window or tab.

- Named Targets: The `target` attribute can also be set to a named target, which is the name of a specific window or frame. When a link with a named target is clicked, the linked resource will open in the same named window, allowing multiple links to be used within the same window. For example, `<a href="https://www.example.com" target="_cow">Example Website</a>` opens the linked website in a window named "_cow".

- No Target Attribute: If there is no `target` attribute specified, the linked resource will open in the same browsing context (same window or frame) as the current page. This will replace the current content with the linked resource. For example, `<a href="https://www.example.com">Example Website</a>` opens the linked website in the current window.