The protocol
property sets or gets
the protocol part of the href attribute value.
protocol |
Yes | Yes | Yes | Yes | Yes |
Return the protocol property:
var v = anchorObject.protocol
Set the protocol property:
anchorObject.protocol=protocol
Value | Description |
---|---|
protocol | Specifies the protocol of a URL. Possible Values:
|
A String representing the protocol part of the URL
The following code shows how to get the protocol of a link.
<!DOCTYPE html>
<html>
<body>
<!-- ww w . ja va2 s. co m-->
<p><a id="myAnchor" href="http://www.example.com/test.htm#part2">Example link</a></p>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myAnchor").protocol;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to change the protocol of a link.
<!DOCTYPE html>
<html>
<body>
<!--from ww w. j a va2 s. com-->
<p><a id="myAnchor" href="http://www.example.com/test.htm#part2">Example link</a></p>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
document.getElementById("myAnchor").protocol = "mailto:";
document.getElementById("demo").innerHTML = "changed";
}
</script>
</body>
</html>
The code above is rendered as follows: