Home arrow JavaScript arrow Page 5 - The DOM Location Object Properties
JAVASCRIPT

The DOM Location Object Properties


In this article we will cover the DOM Location Objects, which are used to set or return information regarding a user’s URL. Often considered an HTML DOM object, it is really more of a JavaScript object, created by the runtime engine. Like other objects, it consists of both Properties and Methods, the former of which we will be discussing today.

Author Info:
By: James Payne
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
December 01, 2008
TABLE OF CONTENTS:
  1. · The DOM Location Object Properties
  2. · Hash It Up
  3. · Being a Good Host
  4. · Using Href
  5. · Getting the Pathname
  6. · Following Protocol

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
The DOM Location Object Properties - Getting the Pathname
(Page 5 of 6 )

As you can see, all of the Location objects are pretty simple to incorporate. Here, we will use pathname to retrieve the path of a URL. Here we will assume the URL is http://www.devshed.com/jamespayneisawesome/pictures.html :


<html>

<body>

<script type="text/javascript">

alert(location.pathname);

</script>

</body>

</html>

Resulting in: jamespayneisawesome/pictures.html

And once again we could also just write it to the page instead of popping up an alert:


<html>

<body>

<script type="text/javascript">

document.write(location.pathname);

</script>

</body>

</html>

Nothing Like a Fine Port

I know what you drunkards and cheese hounds are thinking, and you can stop right in your tracks. The port object, despite its delicious and intoxicating name, will not get you wasted or taste nice on a cracker. It will, however, return or set the portnumber of the current URL, with one exception: if the portnumber is the default value of 80, it will return nothing (if you try this code and get zilch, that is why). Here is the code:


<html>

<body>

<script type="text/javascript">

alert(location.port);

</script>

</body>

</html>

In my browser, that returns a big fat alert box with nothing inside. If you get the same result, then you may be asking yourself, how do I know this guys isn't lying to me? Well you don't. So neener neener. However, if you want to test it and see, try this code:


<html>

<body>

<script type="text/javascript">

alert(location.port+1);

</script>

</body>

</html>

If your portnumber is the default of 80, then your alert box will now display the number 1. It should, by all rights display 81, but for some reason that simply is not the case. If your port number is not 80, then it will display whatever your portnumber is, plus one.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials