The Ugly JavaScript Book - Chapter Fourteen

Chapter Fourteen
The Assorted Location Calls

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

     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:


will set str as follows:




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


     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:


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



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

<TITLE>Using the Location Parsing
<BODY BGCOLOR="white">
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>
<!-- Hide from JS-Impaired Browsers
+'This document\'s <B>location.href</B> '
+'is <FONT COLOR="blue">'
+'<P>This document\'s <B>'
+'tocol</B> is <FONT COLOR="blue">'
+'<P>* This document\'s <B>location.ho'
+'st</B> is <FONT COLOR="blue">''</FONT>'
+'<P>** This document\'s <B>location.ho'
+'stName</B> is <FONT COLOR="blue">'
+'<P>* This document\'s <B>location.port'
+'</B> is <FONT COLOR="blue">'
+'<P>** This document\'s <B>location.path'
+'Name</B> is <FONT COLOR="blue">'
+'<P>* This document\'s <B>location.has'
+'h</B> is <FONT COLOR="blue">'
+'<P>This document\'s <B>location.sea'
+'rch</B> is <FONT COLOR="blue">''?testquery</FONT>'
// End Hiding -->

Click Here to See This Script

Links: Introduction | Chapter One | Chapter Two | Chapter Three | Chapter Four | Chapter Five | Chapter Six | Chapter Seven | Chapter Eight | Chapter Nine | Chapter Ten | Chapter Eleven | Chapter Twelve | Chapter Thirteen | Chapter Fifteen | Chapter Sixteen | Chapter Seventeen | Chapter Eighteen | Chapter Nineteen | Chapter Twenty | Chapter Twenty-one | Chapter Twenty-two | Chapter Twenty-three | Appendix A | Appendix B | Appendix C | Appendix D | Cover
This free virtual book provided courtesy of:
AutoScripter(tm), the site for professional Web Designers and Developers and
WebWalker Virtual Press, virtual publishing on the Web since 1994
Other free e-books from WebWalker: The All American Terrorists | The Gnomes of Jost | For the Love of Entropy | The Trees of Kratva | The Arch of Rain Magicians | Lightshow | The Black Mancutters Guild | The Septoracle

© Copyright 1997, 1999, 2000, 2001 John H. Keyes