URL मॉड्यूल
URL class एक web address को protocol, host, path, और बाकी में तोड़ता है, और नई safely बनाता है।
In this page:
Syntax
const url = new URL(text, base);
url.protocol
url.hostname
url.pathname
url.searchParams
URL Module
new URL(text, base) addresses parse और validate करता है। यह protocol, hostname, port, pathname, search, और hash expose करता है। Properties set करना address को encoding के साथ सही तरीके से rebuild करता है।
Note:
एक invalid URL एक TypeError throw करता है, इसलिए untrusted input को try/catch में wrap करें।
उदाहरण: URL module
const u = new URL("https://[email protected]:8080/a/b?x=1#top");
console.log(u.protocol, u.hostname, u.port);
console.log(u.pathname, u.search, u.hash);
u.searchParams.set("y", "two words");
console.log(u.href);
try { new URL("not a url"); } catch (e) { console.log(e.name); }
// Output:
// https: example.com 8080
// /a/b ?x=1 #top
// https://[email protected]:8080/a/b?x=1&y=two+words#top
// TypeError
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- regular expressions से URLs parse करना
- relative URLs के लिए एक base भूल जाना
- invalid URLs catch न करना
चैप्टर सारांश
- URL addresses parse करता है
- pathname, search, hash expose करता है
- Relative URLs को एक base चाहिए
- Invalid input throw करता है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: