Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q976 How can I extract the directory path from the documents location?

irt.org | Knowledge Base | JavaScript | Link | Q976 [ previous next ]

Q976 How can I extract the directory path from the documents location?

Say you have a location of:

http://www.irt.org:8080/some/where/deep/inside/filename.htm?name=martin#anchorname

This corresponds to:

protocol://hostname:port/pathname?search#hash

Then:

alert(location.href); // displays 'http://www.irt.org:8080/some/where/deep/inside/filename.htm?name=martin#anchorname'

alert(location.protocol); // displays 'http:'
alert(location.hostname); // displays 'www.irt.org'
alert(location.host); // displays 'www.irt.org:8080'
alert(location.port); // displays '8080'
alert(location.pathname); // displays '/some/where/deep/inside/filename.htm'
alert(location.search); // displays '?name=martin'
alert(location.hash); // displays 'anchorname'

To get just the directory path, use:

alert(location.pathname.substring(0,location.pathname.lastIndexOf('/'))); // displays '/some/where/deep/inside'

Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 6th July 2009. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2009 irt.org, All Rights Reserved.