Notes (1) – Obtain URL information using JavaScript

By Nethru Limited (

Do you have a practice to jot down some notes for what you have studied or learnt when you are doing your works? I am going to share my notes to you here, hope that they are useful to you as well.
Let’s start from a simple but common one – obtain URL information using JavaScript.

Suppose you are coding in this URL:
If you put the following JavaScript codes to your page

<script type="text/javascript">
    document.write("location.href: "+location.href+"<br/>");
    document.write("location.protocol: "+location.protocol+"<br/>");
    document.write("location.hostname: "+location.hostname+"<br/>");
    document.write(" ""<br/>");
    document.write("location.port: "+location.port+"<br/>");
    document.write("location.pathname: "+location.pathname+"<br/>");
    document.write(" ""<br/>");
    document.write("location.hash: "+location.hash+"<br/>");

Your page will show:
location.protocol: http
location.port: 8080
location.pathname: /demo/helloworld.php ?user=guest
location.hash: #debug

Leave a Reply

Your email address will not be published. Required fields are marked *