The username
property sets or gets the username
part of the href attribute value.
Example: https://userName:password@www.example.com.
username |
Yes | No | Yes | No | Yes |
Return the username property:
var v = anchorObject.username;
Set the username property:
anchorObject.username = username;
Value | Description |
---|---|
username | Set the username part of a URL |
A String representing the username part of the URL.
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:
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: