JavaScript
  Home arrow JavaScript arrow Page 2 - The DOM Location Object Properties
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

The DOM Location Object Properties
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-12-01

    Table of Contents:
  • The DOM Location Object Properties
  • Hash It Up
  • Being a Good Host
  • Using Href
  • Getting the Pathname
  • Following Protocol

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    The DOM Location Object Properties - Hash It Up


    (Page 2 of 6 )

    As stated in our table above, hash is used to retrieve or set a URL, starting from the hash (#) symbol. If you are up to par on your HTML (and if you are reading this I assume that you are), then you will know that the hash (#) symbol is used for an anchor in a URL. Below, we are going to write some code to retrieve the URL after a hash symbol. For this example we will say that we have a page URL of http://www.devshed.com/example.html#testing.


    <html>

    <body>

    <script type="text/javascript">

    document.write(location.hash);

    </script>

    </body>

    </html>

    Here, the result would be:

      #testing

    If we wanted to set the URL after the hash, we would do so in this manner:


    <html>

    <body>

    <script type="text/javascript">

    location.hash="#chickenhawk";

    document.write(location.hash);

    </script>

    </body>

    </html>

    Here, the URL after the hash would be chickenhawk.

    Let's say that we wanted to make hash a little more complicated. In the following example, we will create two pages. One page will contain some bulleted lists with anchors. The second page will contain buttons that will link to the locations on our first page. Here is the page of lists and anchors, which we will name Chinese.htm:


    <html>

    <body>

    <a name="Chicken">Chicken</a>

    <ul>

    <li>Fried</li>

    <li>Wings</li>

    <li>Orange</li>

    </ul>

    <a name="Appetizers">Appetizers</a>

    <ul>

    <li>Crab Rangoon</li>

    <li>Fried Dumplings</li>

    <li>Egg Rolls</li>

    </ul>

    <a name="Rice">Rice</a>

    <ul>

    <li>White</li>

    <li>Chicken Fried</li>

    </ul>

    </body>

    </html>

    As you can see, we have three anchors: Chicken, Appetizer, and Rice. Each anchor links to a section about different sorts of food. In this instance, Chicken links to a section discussing fried, orange, and wings. Rice links to White and Chicken Fried, etc.

    Next we will create the second page, which has links to the various anchors in our first page:


    <html>

    <head>

    <script type="text/javascript">

    function linker(y)

    {

    var x=window.open("chinese.htm","","width=500,height=400");

    x.location.hash=y;

    }

    </script>

    </head>

    <body>

    <h3>Chinese Anchors</h3>

    <p>Click a button to view the anchor and its list:</p>

    <input type="button" value="Chicken" onclick="linker(Chicken)">

    <input type="button" value="Appetizer" onclick="linker(Appetizer)">

    <input type="button" value="Rice" onclick="linker(Rice)">

    </body>

    </html>

    When you test the page, if you click on the Chicken button, you should see something along the lines of this:

    Chicken

    • Fried

    • Wings

    • Orange

    More JavaScript Articles
    More By James Payne


     

    JAVASCRIPT ARTICLES

    - Comparing Fields and Customizing Error Messa...
    - Checking Numbers and File Extensions with jQ...
    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek