How to use Location Object in JavaScript

In this article I am going to explain about Location object in JavaScript.
  • 1527

JavaScript Location Object

JavaScript Location object is an parts of window object and its contain information about current visit Url. Information Such as Host name, port number, path name etc.

Syntax

location.hash

Example // This example will display host name of current url

<html>

<head>

    <script type="text/javascript">

        function myfun() {

            document.write(location.hostname);

        }

 

    </script>

</head>

<body>

    <button onclick="myfun()">

        click me</button>

</body>

</html>

Output

 hostname.jpg

Example //This example will return port number of current Url

<html>

<head>

    <script type="text/javascript">

        function myfun() {

            document.write("Port No :-" + location.port);

        }

    </script>

</head>

<body>

    <button onclick="myfun()">

        click me</button>

</body>

</html>

Output

portno.jpg

You may also want to read these related articles: here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.