Javascript Reference - HTML DOM Anchor protocol Property








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

Browser Support

protocol Yes Yes Yes Yes Yes

Syntax

Return the protocol property:

var v = anchorObject.protocol 

Set the protocol property:

anchorObject.protocol=protocol

Property Values

Value Description
protocol Specifies the protocol of a URL. Possible Values:
  • file:
  • ftp:
  • http:
  • https:
  • mailto:
  • etc..




Return Value

A String representing the protocol part of the URL

Example

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:





Example 2

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: