JS URL HANDLING
The concept of the URL (Uniform Resource Locator) was developed by Sir Tim Berners-Lee, a British computer scientist. He is best known for inventing the World Wide Web, including the first web browser, server, and URL system. Tim Berners-Lee proposed the idea of a universal naming system for resources on the internet in 1990 while working at CERN (European Organization for Nuclear Research). He outlined the concept in a document titled "Information Management: A Proposal," which laid the foundation for the World Wide Web.
In 1993, the World Wide Web became publicly accessible, and URLs became a fundamental component of navigating and accessing information on the internet. Tim Berners-Lee's contributions played a crucial role in shaping the way we use and access the web today.
?What is a URL?
A URL, or Uniform Resource Locator, is a reference or address used to access resources on the internet. It serves as a web address that specifies the location of a particular resource, such as a webpage, file, or document, on the World Wide Web. URLs are used by web browsers to navigate to and retrieve these resources.
A standard URL typically consists of several components:
an example of url:
As we can see url is a simple chain of string characters that represents important information, but as you may expect there are some concerns.
Handling URLs as simple strings can introduce various challenges and issues due to the complex and structured nature of URLs. Here are some common problems associated with treating URLs as plain strings:
To overcome this issues Javascript has an incredible feature which is the URL class:
The URL class provides a convenient way to parse, manipulate, and construct URLs in a programmatic manner.
Here's a brief overview of the URL class:
Creating a URL Object:
You can create a new URL object by passing a URL string to the constructor:
const urlString = 'https://www.example.com/path?query=param#fragment';
const url = new URL(urlString);
Properties of a URL Object:
Once you have a URL object, you can access various components of the URL using its properties:
领英推荐
console.log(url.href); // https://www.example.com/path?query=param#fragment
console.log(url.protocol); // https:
console.log(url.host); // www.example.com
console.log(url.pathname); // /path
console.log(url.search); // ?query=param console.log(url.hash); // #fragment
Modifying a URL:
You can modify a URL object by directly assigning new values to its properties:
url.protocol = 'http:';
url.hostname = 'newhost.com';
url.pathname = '/newpath';
url.search = '?newquery=value';
url.hash = '#newfragment';
URLSearchParams:
The URLSearchParams interface, accessible through the searchParams property of a URL object, allows you to manipulate the query parameters easily:
const params = new URLSearchParams(url.search); params.append('newparam', 'newvalue');
url.search = params.toString();
Parsing Relative URLs:
The URL class also supports parsing relative URLs:
const base = new URL('https://www.example.com');
const relativeURL = new URL('/relative', base); console.log(relativeURL.href); // https://www.example.com/relative
Ok but what exactly is URLSearchParams and how does it differ from the url search property?
In the context of the URL class in JavaScript, url.search and url.searchParams represent different aspects of a URL's query parameters.
url.search:
const url = new URL('https://example.com/path?param1=value1¶m2=value2'); console.log(url.searchParams.get('param1')); // value1 console.log(url.searchParams.get('param2')); // value2
url.searchParams:
const url = new URL('https://example.com/path?param1=value1¶m2=value2'); console.log(url.searchParams.get('param1')); // value1 console.log(url.searchParams.get('param2')); // value2
You can also modify parameters using methods like set, append, delete, etc.
url.searchParams.set('param1', 'newvalue');
console.log(url.href); // https://example.com/path?param1=newvalue¶m2=value2
In conclusion, url.search gives you the raw query string, while url.searchParams gives you a more convenient interface for working with individual query parameters. Using url.searchParams is often preferable when dealing with dynamic or complex query parameters in JavaScript.
This is a basic overview of the URL class in JavaScript. It's particularly useful when working with URLs in a web development context, allowing you to handle and modify URLs in a standardized way.