Javascript Reference - Location protocol Property








The protocol property sets or gets the protocol of the current URL, including the colon (:).

Browser Support

protocol Yes Yes Yes Yes Yes

Syntax

Return the protocol property.

var v = location.protocol

Set the protocol property.

location.protocol=protocol

Parameter Values

Value Type Description
protocol String The protocol of the URL. Possible values:
  • file:
  • ftp:
  • http:
  • https:
  • mailto:
  • etc..




Return Value

A String type value representing the protocol of the current URL, including the colon (:).

Example

The following code shows how to Return the protocol of the current URL.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<!--from  w  ww  .j a  v a2s  . c  om-->
<script>
function myFunction() {
    var x = location.protocol;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: