Skip to content
the poor coder

javascript to get uri

Poor Man
Published
Reading time
1 min read

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:

Text

  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:

Text

  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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC