Java Reference
In-Depth Information
As with the substring() method, the second parameter is optional. If you don't include it, all the char-
acters from the start position onward will be included.
The substring() method is supported by pre-version 4 browsers, and the substr() method is
supported by version 4 (and later) browsers. Most of the time, you will use the substr() method.
Let's look at the use of the substr() and lastIndexOf() methods together. In the next chapter, you'll
see how you can retrieve the fi le path and name of the currently loaded web page. However, there is
no way of retrieving the fi le name alone. So if, for example, your fi le is http://mywebsite/temp/
myfile.htm, you may need to extract the myfile.htm part. This is where substr() and lastIndexOf()
are useful.
var fileName = window.location.href;
fileName = fileName.substr(fileName.lastIndexOf(“/”) + 1);
document.write(“The file name of this page is “ + fileName);
The fi rst line sets the variable fileName to the current fi le path and name, such as /mywebsite/temp/
myfile.htm. Don't worry about understanding this line; you'll be looking at it in the next chapter.
The second line is where the interesting action is. You can see that this code uses the return value
of the lastIndexOf() method as a parameter for another method, something that's perfectly cor-
rect and very useful. The goal in using fileName.lastIndexOf(“/”) is to fi nd the position of the
fi nal forward slash (/), which will be the last character before the name of the fi le. You add one to this
value, because you don't want to include that character, and then pass this new value to the substr()
method. There's no second parameter here (the length), because you don't know it. As a result, sub-
str() will return all the characters right to the end of the string, which is what you want.
This example retrieves the name of the page on the local machine, because you're not accessing the page
from a web server. However, don't let this mislead you into thinking that accessing fi les on a local hard
drive from a web page is something you'll be able to do with JavaScript alone. To protect users from
malicious hackers, JavaScript's access to the user's system, such as access to fi les, is very limited. You'll
learn more about this later in the topic.
Converting Case — The toLowerCase() and toUpperCase() Methods
If you want to change the case of a string (for example, to remove case sensitivity when comparing
strings), you need the toLowerCase() and toUpperCase() methods. It's not hard to guess what these
two methods do. Both of them return a string that is the value of the string in the String object, but with
its case converted to either upper or lower depending on the method invoked. Any non-alphabetical
characters remain unchanged by these functions.
In the following example, you can see that by changing the case of both strings you can compare them
without case sensitivity being an issue.
var myString = “I Don't Care About Case”
if (myString.toLowerCase() == “i don't care about case”)
{
alert(“Who cares about case?”);
}
Search WWH ::




Custom Search