Location href Property - Javascript Browser Object Model

Javascript examples for Browser Object Model:Location

Description

The href property sets or gets the entire URL of the current page.

Set the href property with the following Values

Value Type Description
URL String Sets the URL of the link.

Possible values:

  • An absolute URL - like location.href="http://www.example.com/default.htm"
  • A relative URL - like location.href="default.htm"
  • An anchor URL - like location.href="#top"
  • A new protocol - like location.href="ftp://someftpserver.com", location.href="mailto:someone@example.com" or location.href="file://host/path/example.txt"

Return Value

A String, representing the entire URL of the page

The following code shows how to return the entire URL of the current page:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//from  ww w  .j  av a2s . c om
    var x = location.href;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials