Online Documentation Server
 ÏÎÈÑÊ
ods.com.ua Web
 ÊÀÒÅÃÎÐÈÈ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ÏÎÄÏÈÑÊÀ

 Î ÊÎÏÈÐÀÉÒÀÕ
Âñÿ ïðåäîñòàâëåííàÿ íà ýòîì ñåðâåðå èíôîðìàöèÿ ñîáðàíà íàìè èç ðàçíûõ èñòî÷íèêîâ. Åñëè Âàì êàæåòñÿ, ÷òî ïóáëèêàöèÿ êàêèõ-òî äîêóìåíòîâ íàðóøàåò ÷üè-ëèáî àâòîðñêèå ïðàâà, ñîîáùèòå íàì îá ýòîì.




Previous Table of Contents Next

Microsoft Internet Explorer and Netscape Navigator deal differently with the location.href property, and with the location object in general. The following document segment stresses the difference:

<HTML>
<HEAD>
<TITLE>location test</TITLE>
</HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--

function load() {
 location.href = "http://www.microsoft.com"
 alert(location.href)
}
// -->
</SCRIPT>
<BODY onLoad="load()">
</BODY>
</HTML>

Suppose this file is located at <URL:http://www.geocities.com>. When Netscape Navigator loads a page, it holds the loaded URL in a cell somewhere in memory. Only when another page has begun loading (data is being transferred) is the value of that cell modified to match the new URL. When you read the value of location.href, you are reading the value of that cell. However, when you assign it a string representing another URL, the value held in that cell does not change immediately. Only if and when the page at the specified URL is found on the server is the value of the cell updated. Microsoft’s browser differs in this case. When the user assigns a string to location.href, it automatically updates the corresponding cell in memory. Therefore, if you display the value of the property location.href immediately after you assign it a value, the assigned value appears. In Navigator, on the other hand, the original URL is still displayed because the file at the new URL has not been found yet. Let’s sum things up. The displayed value of the preceding script on each of the leading browsers is as follows:

  • Netscape Navigator—http://www.geocities.com
  • Microsoft Internet Explorer—http://www.microsoft.com

Depending on your browser, the value of location.href may be encoded with ASCII equivalents of nonalphanumeric characters. Such characters appear as a percent sign (“%”) followed by the ASCII code of that character. The most commonly encoded character is the space, “%20.” You can run such URLs under the unescape() function to convert them to ISO Latin-1 format.

Suppose you have an HTML file named foo.html located in a certain directory, better known as a folder on Macs and Windows95. Loading the full path of this directory in the browser should normally show the listing of all the directory’s files, provided that a default filename supported by the server is not included in that directory. You can use the following script to allow the user to view the listing of the files just by clicking a button:

<HTML>
<HEAD>
<TITLE>Directory listing</TITLE>
</HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--

function getListing() {
 var url = location.href
 var lastSlash = url.lastIndexOf("/")
 location.href = url.substring(0, lastSlash + 1)
}
// -->
</SCRIPT>
<BODY>
<FORM>
<INPUT TYPE="button" VALUE=" view directory
listing " onClick="getListing()">
</FORM>
</BODY>
</HTML>

There is no need to go over this script because its backbone was already explained. It strips off the filename and loads the string as the new URL. Once again, MSIE has problems with this script on PCs, due to the backslash versus slash URL specification differences. Here is a screen capture of a directory listing retrieved by clicking on the button:


Figure 19-1.  A sample directory listing.

hash

An anchor is a mark for other data to point to. It enables you to create a link to a specific place somewhere in a given Web page. Suppose you have a Web page that provides information on various VCR models of different brands. A user who is looking for specific information on Sony VCRs should not be penalized by having to scroll through other makes, say JVCs. Therefore, you can create a named anchor somewhere near the name Sony. An anchor is created using the following syntax:

<A NAME="sony1">Sony VCRs</A>

The text “Sony VCRs” appears on the page as normal, but it serves as an anchor. You can direct the user to “Sony VCRs” section via a simple link to the anchor, in the following fashion:

<A HREF="#sony1">Get information on Sony VCRs</A>

In this case, the link’s URL is the anchor’s name preceded by a hash mark (#). When the user clicks on the link “Get information on Sony VCRs,” the browser automatically “scrolls” down to the anchor named “sony1” (you can’t see the “scrolling” of course). You can also direct the user to that anchor from within another page. For example, suppose the full URL of the VCR page is <URL:http://www.vcr.com/ information/index.html>. Now, let’s say you want to provide a link from the page <URL:http://www.electronics.com/VCRlinks/new.html> to the page containing information on VCRs, and, in particular, to the Sony section. You can accomplish this task by including the URL of the VCR file, as well as the Sony anchor name, somewhere in the electronics file:

<A HREF=" http://www.vcr.com/information/index.html#sony1">Get information on Sony VCRs</A>

This form enables you to specify the document URL as well as the specific anchor name. By specifying only the anchor name, it is assumed that the anchor resides in the current document, just as if you specify a filename without a full path, it is assumed to reside in the same directory as the HTML document. Such URL references are known as relative or partial ones.

When you click on a link to a URL containing a named anchor reference, the new URL, or location, consists of the hash mark (#) followed by the anchor name. This portion of the URL is considered a part of the URL, just like the path or filename. After linking to an anchor, user-initiated scrolling does not affect the URL (or the location.href value).

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us