javascript to get uri

How to Get URI Using JavaScript?

URI or Uniform Resource Identifier is a string of characters that identifies a name or a resource on the internet. In JavaScript, you can use the window.location.href property to get the current URI of a web page. Here's how you can do it:


  var currentURI = window.location.href;
  console.log(currentURI);

The above code will log the current URI of the web page to the console. You can use this URI for various purposes, such as sending it as a parameter in an AJAX request or displaying it to the user.

Other Ways to Get the URI

There are other ways to get the URI using JavaScript. Here are a few:

  • window.location.pathname: This property returns the path of the current URL.
  • window.location.protocol: This property returns the protocol of the current URL (HTTP or HTTPS).
  • window.location.host: This property returns the hostname and port number of the current URL.

Here's an example code snippet that demonstrates the use of these properties:


  var currentPath = window.location.pathname;
  var currentProtocol = window.location.protocol;
  var currentHost = window.location.host;

  console.log('Path: ' + currentPath);
  console.log('Protocol: ' + currentProtocol);
  console.log('Host: ' + currentHost);

The above code will log the path, protocol, and host of the current URL to the console. You can use these properties to extract specific parts of the URL for your application's needs.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe