URL Objects
Network Requests: URL Objects
What is a URL object in JavaScript?
View Answer:
new URL(url)
new URL(url, base)
// Mozilla Example
let B = new URL(baseUrl);
// => 'https://developer.mozilla.org/'
new URL("en-US/docs", B);
// => 'https://developer.mozilla.org/en-US/docs'
What does the 'href' property do in a URL object?
View Answer:
- Creating a new URL object
let url = new URL("https://www.example.com/path?query=example#fragment");
console.log(url.href);
// Output: "https://www.example.com/path?query=example#fragment"
- Modifying an existing URL
let url = new URL("https://www.example.com");
url.href = "https://www.newexample.com/newpath?newquery=newexample#newfragment";
console.log(url.href);
// Output: "https://www.newexample.com/newpath?newquery=newexample#newfragment"
As you can see, the href
property can be used to both read the full URL, and set a new URL. Note that when you modify the href
of a URL object, all the other properties (like protocol
, host
, pathname
, search
, and hash
) get updated as well.
What does the protocol property in a URL object represent?
View Answer:
let url = new URL("https://www.example.com/path?query=example#fragment");
url.host = "www.newexample.com:9090";
console.log(url.protocol);
// Output: "https:"
console.log(url.href);
// Output: "https://www.newexample.com:9090/path?query=example#fragment"
What is the host property in a URL object?
View Answer:
let url = new URL("https://www.example.com/path?query=example#fragment");
url.host = "www.newexample.com:9090";
console.log(url.host);
// Output: "www.newexample.com:9090"
console.log(url.href);
// Output: "https://www.newexample.com:9090/path?query=example#fragment"
What is the pathname property in a URL object?
View Answer:
let url = new URL("https://www.example.com/path?query=example#fragment");
url.host = "www.newexample.com:9090";
console.log(url.pathname);
// Output: "/path"
console.log(url.href);
// Output: "https://www.newexample.com:9090/path?query=example#fragment"
What does the search property do in a URL object?
View Answer:
let url = new URL("https://www.example.com/path?query=example#fragment");
url.host = "www.newexample.com:9090";
console.log(url.search);
// Output: "?query=example"
console.log(url.href);
// Output: "https://www.newexample.com:9090/path?query=example#fragment"
What does the hash property represent in a URL object?
View Answer:
- Creating a new URL object and accessing the hash
let url = new URL("https://www.example.com/path?query=example#fragment");
console.log(url.hash);
// Output: "#fragment"
- Modifying the hash of an existing URL
let url = new URL("https://www.example.com/path?query=example");
url.hash = "newfragment";
console.log(url.hash);
// Output: "#newfragment"
console.log(url.href);
// Output: "https://www.example.com/path?query=example#newfragment"
Note that when you modify the hash of a URL object, the href property is updated to reflect the new URL.
What does the port property represent in a URL object?
View Answer:
- Creating a new URL object and accessing the port
let url = new URL("https://www.example.com:8080/path?query=example#fragment");
console.log(url.port);
// Output: "8080"
- Modifying the port of an existing URL
let url = new URL("https://www.example.com/path?query=example");
url.port = "9090";
console.log(url.port);
// Output: "9090"
console.log(url.href);
// Output: "https://www.example.com:9090/path?query=example"
Note that when you modify the hash of a URL object, the href property is updated to reflect the new URL.
What is the purpose of the username and password properties in a URL object?
View Answer:
- Creating a new URL object and accessing the username and password
let url = new URL("https://username:password@example.com/path?query=example#fragment");
console.log(url.username); // Output: "username"
console.log(url.password); // Output: "password"
- Modifying the username and password of an existing URL
let url = new URL("https://example.com/path?query=example");
url.username = "newusername";
url.password = "newpassword";
console.log(url.username); // Output: "newusername"
console.log(url.password); // Output: "newpassword"
console.log(url.href);
// Output: "https://newusername:newpassword@example.com/path?query=example"
Note that usage of passwords directly in URLs is generally not recommended due to security reasons.
What is the origin property in a URL object?
View Answer:
- Creating a new URL object and accessing the origin
let url = new URL("https://www.example.com:8080/path?query=example#fragment");
console.log(url.origin);
// Output: "https://www.example.com:8080"
Note that origin
property is read-only. You cannot modify the origin
directly; instead, you can modify the protocol
, hostname
, or port
properties individually, and the origin
will update accordingly.
What is the purpose of the URLSearchParams interface?
View Answer:
- Creating a new URL object and accessing the search parameters
let url = new URL("https://www.example.com/path?query1=example1&query2=example2#fragment");
let params = new URLSearchParams(url.search);
console.log(params.get('query1')); // Output: "example1"
console.log(params.get('query2')); // Output: "example2"
- Modifying the search parameters of an existing URL
let url = new URL("https://www.example.com/path?query=example");
let params = new URLSearchParams(url.search);
params.set('query', 'newexample');
params.append('newquery', 'example');
url.search = params.toString();
console.log(url.href);
// Output: "https://www.example.com/path?query=newexample&newquery=example"
The URLSearchParams
interface is very useful for manipulating the query string of a URL without having to deal with string parsing and serialization yourself.
What is the purpose of the built-in URL class?
View Answer:
Syntax: new URL(url, [base]);
// These two URLs are same:
let url1 = new URL('https://javascript.info/profile/admin');
let url2 = new URL('/profile/admin', 'https://javascript.info');
console.log(url1); // https://javascript.info/profile/admin
console.log(url2); // https://javascript.info/profile/admin
// We can easily create a new URL based on
// the path relative to an existing URL:
let url = new URL('https://javascript.info/profile/admin');
let newUrl = new URL('tester', url);
console.log(newUrl); // https://javascript.info/profile/tester
// The URL object immediately allows us to access its components
let url = new URL('https://javascript.info/url');
console.log(url.protocol); // https:
console.log(url.host); // javascript.info
console.log(url.pathname); // /url
Can we pass URL objects to APIs and other methods instead of a string?
View Answer:
Here is a Node.js example using the http
module and the URL
object:
const http = require('http');
const url = new URL('http://example.com');
http.get(url, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log(data);
});
}).on('error', (err) => {
console.log("Error: " + err.message);
});
In this code, the http.get
method accepts a URL
object (url
) as its first argument. Note that support for URL
objects in APIs depends on the language and library.
Is there a way to parse data inside a URL string?
View Answer:
let params = new URL(document.location).searchParams;
let name = params.get('name'); // is the string "Jonathan Smith".
let age = parseInt(params.get('age')); // is the number 18
Can you explain the function of the URLSearchParams.get() method?
View Answer:
let params = new URL(document.location).searchParams;
let name = params.get('name'); // is the string "Jonathan Smith".
let age = parseInt(params.get('age')); // is the number 18
Which standard defines which characters are allowed in URLs?
View Answer:
// using some cyrillic characters for this example
let url = new URL('https://ru.wikipedia.org/wiki/Тест');
url.searchParams.set('key', 'ъ');
console.log(url); //https://ru.wikipedia.org/wiki/%D0%A2%D0%B5%D1%81%D1%82?key=%D1%8A
What is the difference between encodeURIComponent and encodeURI?
View Answer:
// For URL parameters we should use encodeURIComponent instead
let music = encodeURIComponent('Rock&Roll');
let url = `https://google.com/search?q=${music}`;
console.log(url); // https://google.com/search?q=Rock%26Roll
// Compare it with encodeURI
let music = encodeURI('Rock&Roll');
let url = `https://google.com/search?q=${music}`;
console.log(url); // https://google.com/search?q=Rock&Roll
We should note that encoding can be a bit touchy, and you should pay attention to any characters that encodeURI can misinterpret.
Is there a difference between URL transformation and the 'encoding' functions?
View Answer:
// valid url with IPv6 address
let url = 'http://[2607:f8b0:4005:802::1007]/';
console.log(encodeURI(url)); // http://%5B2607:f8b0:4005:802::1007%5D/
console.log(new URL(url)); // http://[2607:f8b0:4005:802::1007]/