Chapter Fourteen
The Assorted Location Calls


Or, "Where Am I and How Did I Get Here?" or, "Where Do I Go Now?"

variable=location.href;

This call gives you the entire URL location at this moment for the document now being displayed on screen.

It is important to know that this URL is platform independent. If you are viewing a local file on your own computer, it will return the entire file path on your system.

If you are viewing a file on the internet, it will generally take the form of:

str=location.href;

will set str as follows:

str="http://www.infohiway.com/index.htm";

or

str="ftp://infohiway.com/index.htm";

But if you are viewing a file on your own system, it will take the general form of:

str="file:///Harddrive/JavaScript/index.htm";

Note that the local path begins with file: and is followed by three slashes, while the internet paths are followed by just two slashes.

This is the very same information displayed on the location: line at the top of your browser.

You may change the at any time you wish from JavaScript, just as you can type a different URL or file path on the location: line manually.

Built-in Calls that Parse the .href

Now, of course you can simply parse the entire path returned by the browser into its component parts, using string syntax. However, there are built-in parsers that will return the various parts without you having to do so:

location.protocol
location.host
location.hostName
location.port
location.pathname
location.hash
location.search

Using these location calls, this location.href is parsed into its various components automatically as shown just below:

http://www.infohiway.com:80/js/a.htm#top?x=1

returns:

location.protocol=http:
location.host=www.infohiway.com
location.hostName=www.infohiway.com
location.port=:80
location.pathname=/js/a.htm
location.hash=#top
location.search=?x=1

This little script incorporates the various location calls so that you can see them in action:

<HTML><HEAD>
<TITLE>Using the Location Parsing
Calls</TITLE></HEAD>
<BODY BGCOLOR="white">
<CENTER>
<TABLE BORDER=0 WIDTH=486>
<TR><TD ALIGN=CENTER><P><B>JavaScript
Calls to Parse the Location</B>
<BR>(A virtual search string has been added for
demo only. If being run locally, items with "*"
will display nothing while those with ** will
display "undefined")<P>
</TD></TR>
<P><SCRIPT LANGUAGE="JavaScript">
<!-- Hide from JS-Impaired Browsers
document.write('<TR><TD>'
+'This document\'s <B>location.href</B> '
+'is <FONT COLOR="blue">'
+location.href+'?testquery</FONT>'
+'<P>This document\'s <B>location.pro'
+'tocol</B> is <FONT COLOR="blue">'
+location.protocol+'</FONT>'
+'<P>* This document\'s <B>location.ho'
+'st</B> is <FONT COLOR="blue">'
+location.host+'</FONT>'
+'<P>** This document\'s <B>location.ho'
+'stName</B> is <FONT COLOR="blue">'
+location.hostName+'</FONT>'
+'<P>* This document\'s <B>location.port'
+'</B> is <FONT COLOR="blue">'
+location.port+'</FONT>'
+'<P>** This document\'s <B>location.path'
+'Name</B> is <FONT COLOR="blue">'
+location.pathName+'</FONT>'
+'<P>* This document\'s <B>location.has'
+'h</B> is <FONT COLOR="blue">'
+location.hash+'</FONT>'
+'<P>This document\'s <B>location.sea'
+'rch</B> is <FONT COLOR="blue">'
+location.search+'?testquery</FONT>'
+'</FONT></TD></TR></TABLE>');
// End Hiding -->
</SCRIPT>
</BODY>
</HTML>

Click Here to See This Script



© Copyright 1997, John H. Keyes john.keyes@intellink.net