Javascript Reference - HTML DOM Anchor username Property








The username property sets or gets the username part of the href attribute value.

Example: https://userName:password@www.example.com.

Browser Support

username Yes No Yes No Yes

Syntax

Return the username property:

var v = anchorObject.username;

Set the username property:

anchorObject.username = username;

Property Values

Value Description
username Set the username part of a URL




Return Value

A String representing the username part of the URL.

Example

The following code shows how to change the username part of a link.


<!DOCTYPE html>
<html>
<body>
<!-- w w  w. j  av  a2 s . co  m-->
<p><a id="myAnchor" href="https://userName:password@www.example.com">Example link</a></p>
<button onclick="myFunction()">test</button>
<p id="demo"></p>

<script>
function myFunction() {
    document.getElementById("myAnchor").username = "newName";
    document.getElementById("demo").innerHTML = "changed";
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to get the username part of a link.


<!DOCTYPE html>
<html>
<body>
<!--   www.  j  av a  2s  .  co  m-->
<p><a id="myAnchor" href="https://userName:password@www.example.com">Example link</a></p>
<button onclick="myFunction()">test</button>
<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myAnchor").username;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: