Computer >> Computer tutorials >  >> Programming >> Javascript

How to get the protocol and page path of the current web page in JavaScript?


Javascript has provided the location object to get any details regarding the current web page. This location object has provided window.location.protocol and window.location.pathname to get the protocol and pathname of the current web page respectively. Let's discuss them in detail.

Pathname

Any website will have a pathname. Javascript location object has provided window.location.pathname to get that pathname.

Example

<html>
<body>
<p id="pathname"></p>
<script>
   document.getElementById("pathname").innerHTML =
   "Page path is: " + window.location.pathname;
</script>
</body>
</html>

Output

Page path is: /cg/assets/jgCOmn.php

Protocol

It is common that any site will start with https://. It is nothing but the protocol of a site. This protocol defines how messages are formatted and transmitted, and what actions Web servers and browsers should take in response to various commands.

Example

<html>
<body>
<p id="protoname"></p>
<script>
   document.getElementById("protoname").innerHTML =
   "Protocol name is: " + window.location.protocol;
</script>
</body>
</html>

Output

The page protocol is: https: