1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<meta name="nodejs.org:node-version" content="v22.14.0">
<title>URL | Node.js v22.14.0 Documentation</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
<link rel="stylesheet" href="assets/style.css">
<link rel="stylesheet" href="assets/hljs.css">
<link rel="canonical" href="https://nodejs.org/api/url.html">
<script async defer src="assets/api.js" type="text/javascript"></script>
<script>
const storedTheme = localStorage.getItem('theme');
// Follow operating system theme preference
if (storedTheme === null && window.matchMedia) {
const mq = window.matchMedia('(prefers-color-scheme: dark)');
if (mq.matches) {
document.documentElement.classList.add('dark-mode');
}
} else if (storedTheme === 'dark') {
document.documentElement.classList.add('dark-mode');
}
</script>
<style>@media(max-width:478px){.with-32-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:726px){.with-63-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:750px){.with-66-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:606px){.with-48-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:614px){.with-49-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}</style>
</head>
<body class="alt apidoc" id="api-section-url">
<a href="#apicontent" class="skip-to-content">Skip to content</a>
<div id="content" class="clearfix">
<div role="navigation" id="column2" class="interior">
<div id="intro" class="interior">
<a href="/" title="Go back to the home page">
Node.js
</a>
</div>
<ul>
<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
</ul>
<hr class="line">
<ul>
<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
<li><a href="console.html" class="nav-console">Console</a></li>
<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
<li><a href="dns.html" class="nav-dns">DNS</a></li>
<li><a href="domain.html" class="nav-domain">Domain</a></li>
<li><a href="errors.html" class="nav-errors">Errors</a></li>
<li><a href="events.html" class="nav-events">Events</a></li>
<li><a href="fs.html" class="nav-fs">File system</a></li>
<li><a href="globals.html" class="nav-globals">Globals</a></li>
<li><a href="http.html" class="nav-http">HTTP</a></li>
<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
<li><a href="https.html" class="nav-https">HTTPS</a></li>
<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
<li><a href="typescript.html" class="nav-typescript">Modules: TypeScript</a></li>
<li><a href="net.html" class="nav-net">Net</a></li>
<li><a href="os.html" class="nav-os">OS</a></li>
<li><a href="path.html" class="nav-path">Path</a></li>
<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
<li><a href="process.html" class="nav-process">Process</a></li>
<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
<li><a href="readline.html" class="nav-readline">Readline</a></li>
<li><a href="repl.html" class="nav-repl">REPL</a></li>
<li><a href="report.html" class="nav-report">Report</a></li>
<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
<li><a href="sqlite.html" class="nav-sqlite">SQLite</a></li>
<li><a href="stream.html" class="nav-stream">Stream</a></li>
<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
<li><a href="test.html" class="nav-test">Test runner</a></li>
<li><a href="timers.html" class="nav-timers">Timers</a></li>
<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
<li><a href="tty.html" class="nav-tty">TTY</a></li>
<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
<li><a href="url.html" class="nav-url active">URL</a></li>
<li><a href="util.html" class="nav-util">Utilities</a></li>
<li><a href="v8.html" class="nav-v8">V8</a></li>
<li><a href="vm.html" class="nav-vm">VM</a></li>
<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
</ul>
<hr class="line">
<ul>
<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
</ul>
</div>
<div id="column1" data-id="url" class="interior">
<header class="header">
<div class="header-container">
<h1>Node.js v22.14.0 documentation</h1>
<button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
<svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
<path fill="none" d="M0 0h24v24H0z" />
<path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
<path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
<path d="M0 0h24v24H0z" fill="none" />
<path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
</svg>
</button>
</div>
<div id="gtoc">
<ul>
<li class="pinned-header">Node.js v22.14.0</li>
<li class="picker-header">
<a href="#toc-picker" aria-controls="toc-picker">
<span class="picker-arrow"></span>
Table of contents
</a>
<div class="picker" tabindex="-1"><div class="toc"><ul id="toc-picker">
<li><span class="stability_2"><a href="#url">URL</a></span>
<ul>
<li><a href="#url-strings-and-url-objects">URL strings and URL objects</a>
<ul>
<li><a href="#constructing-a-url-from-component-parts-and-getting-the-constructed-string">Constructing a URL from component parts and getting the constructed string</a></li>
</ul>
</li>
<li><a href="#the-whatwg-url-api">The WHATWG URL API</a>
<ul>
<li><a href="#class-url">Class: <code>URL</code></a>
<ul>
<li><a href="#new-urlinput-base"><code>new URL(input[, base])</code></a></li>
<li><a href="#urlhash"><code>url.hash</code></a></li>
<li><a href="#urlhost"><code>url.host</code></a></li>
<li><a href="#urlhostname"><code>url.hostname</code></a></li>
<li><a href="#urlhref"><code>url.href</code></a></li>
<li><a href="#urlorigin"><code>url.origin</code></a></li>
<li><a href="#urlpassword"><code>url.password</code></a></li>
<li><a href="#urlpathname"><code>url.pathname</code></a></li>
<li><a href="#urlport"><code>url.port</code></a></li>
<li><a href="#urlprotocol"><code>url.protocol</code></a>
<ul>
<li><a href="#special-schemes">Special schemes</a></li>
</ul>
</li>
<li><a href="#urlsearch"><code>url.search</code></a></li>
<li><a href="#urlsearchparams"><code>url.searchParams</code></a></li>
<li><a href="#urlusername"><code>url.username</code></a></li>
<li><a href="#urltostring"><code>url.toString()</code></a></li>
<li><a href="#urltojson"><code>url.toJSON()</code></a></li>
<li><span class="stability_1"><a href="#urlcreateobjecturlblob"><code>URL.createObjectURL(blob)</code></a></span></li>
<li><span class="stability_1"><a href="#urlrevokeobjecturlid"><code>URL.revokeObjectURL(id)</code></a></span></li>
<li><a href="#urlcanparseinput-base"><code>URL.canParse(input[, base])</code></a></li>
<li><a href="#urlparseinput-base"><code>URL.parse(input[, base])</code></a></li>
</ul>
</li>
<li><a href="#class-urlsearchparams">Class: <code>URLSearchParams</code></a>
<ul>
<li><a href="#new-urlsearchparams"><code>new URLSearchParams()</code></a></li>
<li><a href="#new-urlsearchparamsstring"><code>new URLSearchParams(string)</code></a></li>
<li><a href="#new-urlsearchparamsobj"><code>new URLSearchParams(obj)</code></a></li>
<li><a href="#new-urlsearchparamsiterable"><code>new URLSearchParams(iterable)</code></a></li>
<li><a href="#urlsearchparamsappendname-value"><code>urlSearchParams.append(name, value)</code></a></li>
<li><a href="#urlsearchparamsdeletename-value"><code>urlSearchParams.delete(name[, value])</code></a></li>
<li><a href="#urlsearchparamsentries"><code>urlSearchParams.entries()</code></a></li>
<li><a href="#urlsearchparamsforeachfn-thisarg"><code>urlSearchParams.forEach(fn[, thisArg])</code></a></li>
<li><a href="#urlsearchparamsgetname"><code>urlSearchParams.get(name)</code></a></li>
<li><a href="#urlsearchparamsgetallname"><code>urlSearchParams.getAll(name)</code></a></li>
<li><a href="#urlsearchparamshasname-value"><code>urlSearchParams.has(name[, value])</code></a></li>
<li><a href="#urlsearchparamskeys"><code>urlSearchParams.keys()</code></a></li>
<li><a href="#urlsearchparamssetname-value"><code>urlSearchParams.set(name, value)</code></a></li>
<li><a href="#urlsearchparamssize"><code>urlSearchParams.size</code></a></li>
<li><a href="#urlsearchparamssort"><code>urlSearchParams.sort()</code></a></li>
<li><a href="#urlsearchparamstostring"><code>urlSearchParams.toString()</code></a></li>
<li><a href="#urlsearchparamsvalues"><code>urlSearchParams.values()</code></a></li>
<li><a href="#urlsearchparamssymboliterator"><code>urlSearchParams[Symbol.iterator]()</code></a></li>
</ul>
</li>
<li><a href="#urldomaintoasciidomain"><code>url.domainToASCII(domain)</code></a></li>
<li><a href="#urldomaintounicodedomain"><code>url.domainToUnicode(domain)</code></a></li>
<li><a href="#urlfileurltopathurl-options"><code>url.fileURLToPath(url[, options])</code></a></li>
<li><a href="#urlformaturl-options"><code>url.format(URL[, options])</code></a></li>
<li><a href="#urlpathtofileurlpath-options"><code>url.pathToFileURL(path[, options])</code></a></li>
<li><a href="#urlurltohttpoptionsurl"><code>url.urlToHttpOptions(url)</code></a></li>
</ul>
</li>
<li><span class="stability_3"><a href="#legacy-url-api">Legacy URL API</a></span>
<ul>
<li><span class="stability_3"><a href="#legacy-urlobject">Legacy <code>urlObject</code></a></span>
<ul>
<li><a href="#urlobjectauth"><code>urlObject.auth</code></a></li>
<li><a href="#urlobjecthash"><code>urlObject.hash</code></a></li>
<li><a href="#urlobjecthost"><code>urlObject.host</code></a></li>
<li><a href="#urlobjecthostname"><code>urlObject.hostname</code></a></li>
<li><a href="#urlobjecthref"><code>urlObject.href</code></a></li>
<li><a href="#urlobjectpath"><code>urlObject.path</code></a></li>
<li><a href="#urlobjectpathname"><code>urlObject.pathname</code></a></li>
<li><a href="#urlobjectport"><code>urlObject.port</code></a></li>
<li><a href="#urlobjectprotocol"><code>urlObject.protocol</code></a></li>
<li><a href="#urlobjectquery"><code>urlObject.query</code></a></li>
<li><a href="#urlobjectsearch"><code>urlObject.search</code></a></li>
<li><a href="#urlobjectslashes"><code>urlObject.slashes</code></a></li>
</ul>
</li>
<li><span class="stability_3"><a href="#urlformaturlobject"><code>url.format(urlObject)</code></a></span></li>
<li><span class="stability_0"><a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse(urlString[, parseQueryString[, slashesDenoteHost]])</code></a></span></li>
<li><span class="stability_3"><a href="#urlresolvefrom-to"><code>url.resolve(from, to)</code></a></span></li>
</ul>
</li>
<li><a href="#percent-encoding-in-urls">Percent-encoding in URLs</a>
<ul>
<li><a href="#legacy-api">Legacy API</a></li>
<li><a href="#whatwg-api">WHATWG API</a></li>
</ul>
</li>
</ul>
</li>
</ul></div></div>
</li>
<li class="picker-header">
<a href="#gtoc-picker" aria-controls="gtoc-picker">
<span class="picker-arrow"></span>
Index
</a>
<div class="picker" tabindex="-1" id="gtoc-picker"><ul>
<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
<li>
<a href="index.html">Index</a>
</li>
</ul>
<hr class="line">
<ul>
<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
<li><a href="console.html" class="nav-console">Console</a></li>
<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
<li><a href="dns.html" class="nav-dns">DNS</a></li>
<li><a href="domain.html" class="nav-domain">Domain</a></li>
<li><a href="errors.html" class="nav-errors">Errors</a></li>
<li><a href="events.html" class="nav-events">Events</a></li>
<li><a href="fs.html" class="nav-fs">File system</a></li>
<li><a href="globals.html" class="nav-globals">Globals</a></li>
<li><a href="http.html" class="nav-http">HTTP</a></li>
<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
<li><a href="https.html" class="nav-https">HTTPS</a></li>
<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
<li><a href="typescript.html" class="nav-typescript">Modules: TypeScript</a></li>
<li><a href="net.html" class="nav-net">Net</a></li>
<li><a href="os.html" class="nav-os">OS</a></li>
<li><a href="path.html" class="nav-path">Path</a></li>
<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
<li><a href="process.html" class="nav-process">Process</a></li>
<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
<li><a href="readline.html" class="nav-readline">Readline</a></li>
<li><a href="repl.html" class="nav-repl">REPL</a></li>
<li><a href="report.html" class="nav-report">Report</a></li>
<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
<li><a href="sqlite.html" class="nav-sqlite">SQLite</a></li>
<li><a href="stream.html" class="nav-stream">Stream</a></li>
<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
<li><a href="test.html" class="nav-test">Test runner</a></li>
<li><a href="timers.html" class="nav-timers">Timers</a></li>
<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
<li><a href="tty.html" class="nav-tty">TTY</a></li>
<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
<li><a href="url.html" class="nav-url active">URL</a></li>
<li><a href="util.html" class="nav-util">Utilities</a></li>
<li><a href="v8.html" class="nav-v8">V8</a></li>
<li><a href="vm.html" class="nav-vm">VM</a></li>
<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
</ul>
<hr class="line">
<ul>
<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
</ul></div>
</li>
<li class="picker-header">
<a href="#alt-docs" aria-controls="alt-docs">
<span class="picker-arrow"></span>
Other versions
</a>
<div class="picker" tabindex="-1"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v23.x/api/url.html">23.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v22.x/api/url.html">22.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v21.x/api/url.html">21.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v20.x/api/url.html">20.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v19.x/api/url.html">19.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v18.x/api/url.html">18.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v17.x/api/url.html">17.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v16.x/api/url.html">16.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v15.x/api/url.html">15.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v14.x/api/url.html">14.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v13.x/api/url.html">13.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v12.x/api/url.html">12.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v11.x/api/url.html">11.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v10.x/api/url.html">10.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v9.x/api/url.html">9.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v8.x/api/url.html">8.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v7.x/api/url.html">7.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v6.x/api/url.html">6.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v5.x/api/url.html">5.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v4.x/api/url.html">4.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/url.html">0.12.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/url.html">0.10.x</a></li></ol></div>
</li>
<li class="picker-header">
<a href="#options-picker" aria-controls="options-picker">
<span class="picker-arrow"></span>
Options
</a>
<div class="picker" tabindex="-1">
<ul id="options-picker">
<li>
<a href="all.html">View on single page</a>
</li>
<li>
<a href="url.json">View as JSON</a>
</li>
<li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/url.md">Edit on GitHub</a></li>
</ul>
</div>
</li>
</ul>
</div>
<hr>
</header>
<details role="navigation" id="toc" open><summary>Table of contents</summary><ul>
<li><span class="stability_2"><a href="#url">URL</a></span>
<ul>
<li><a href="#url-strings-and-url-objects">URL strings and URL objects</a>
<ul>
<li><a href="#constructing-a-url-from-component-parts-and-getting-the-constructed-string">Constructing a URL from component parts and getting the constructed string</a></li>
</ul>
</li>
<li><a href="#the-whatwg-url-api">The WHATWG URL API</a>
<ul>
<li><a href="#class-url">Class: <code>URL</code></a>
<ul>
<li><a href="#new-urlinput-base"><code>new URL(input[, base])</code></a></li>
<li><a href="#urlhash"><code>url.hash</code></a></li>
<li><a href="#urlhost"><code>url.host</code></a></li>
<li><a href="#urlhostname"><code>url.hostname</code></a></li>
<li><a href="#urlhref"><code>url.href</code></a></li>
<li><a href="#urlorigin"><code>url.origin</code></a></li>
<li><a href="#urlpassword"><code>url.password</code></a></li>
<li><a href="#urlpathname"><code>url.pathname</code></a></li>
<li><a href="#urlport"><code>url.port</code></a></li>
<li><a href="#urlprotocol"><code>url.protocol</code></a>
<ul>
<li><a href="#special-schemes">Special schemes</a></li>
</ul>
</li>
<li><a href="#urlsearch"><code>url.search</code></a></li>
<li><a href="#urlsearchparams"><code>url.searchParams</code></a></li>
<li><a href="#urlusername"><code>url.username</code></a></li>
<li><a href="#urltostring"><code>url.toString()</code></a></li>
<li><a href="#urltojson"><code>url.toJSON()</code></a></li>
<li><span class="stability_1"><a href="#urlcreateobjecturlblob"><code>URL.createObjectURL(blob)</code></a></span></li>
<li><span class="stability_1"><a href="#urlrevokeobjecturlid"><code>URL.revokeObjectURL(id)</code></a></span></li>
<li><a href="#urlcanparseinput-base"><code>URL.canParse(input[, base])</code></a></li>
<li><a href="#urlparseinput-base"><code>URL.parse(input[, base])</code></a></li>
</ul>
</li>
<li><a href="#class-urlsearchparams">Class: <code>URLSearchParams</code></a>
<ul>
<li><a href="#new-urlsearchparams"><code>new URLSearchParams()</code></a></li>
<li><a href="#new-urlsearchparamsstring"><code>new URLSearchParams(string)</code></a></li>
<li><a href="#new-urlsearchparamsobj"><code>new URLSearchParams(obj)</code></a></li>
<li><a href="#new-urlsearchparamsiterable"><code>new URLSearchParams(iterable)</code></a></li>
<li><a href="#urlsearchparamsappendname-value"><code>urlSearchParams.append(name, value)</code></a></li>
<li><a href="#urlsearchparamsdeletename-value"><code>urlSearchParams.delete(name[, value])</code></a></li>
<li><a href="#urlsearchparamsentries"><code>urlSearchParams.entries()</code></a></li>
<li><a href="#urlsearchparamsforeachfn-thisarg"><code>urlSearchParams.forEach(fn[, thisArg])</code></a></li>
<li><a href="#urlsearchparamsgetname"><code>urlSearchParams.get(name)</code></a></li>
<li><a href="#urlsearchparamsgetallname"><code>urlSearchParams.getAll(name)</code></a></li>
<li><a href="#urlsearchparamshasname-value"><code>urlSearchParams.has(name[, value])</code></a></li>
<li><a href="#urlsearchparamskeys"><code>urlSearchParams.keys()</code></a></li>
<li><a href="#urlsearchparamssetname-value"><code>urlSearchParams.set(name, value)</code></a></li>
<li><a href="#urlsearchparamssize"><code>urlSearchParams.size</code></a></li>
<li><a href="#urlsearchparamssort"><code>urlSearchParams.sort()</code></a></li>
<li><a href="#urlsearchparamstostring"><code>urlSearchParams.toString()</code></a></li>
<li><a href="#urlsearchparamsvalues"><code>urlSearchParams.values()</code></a></li>
<li><a href="#urlsearchparamssymboliterator"><code>urlSearchParams[Symbol.iterator]()</code></a></li>
</ul>
</li>
<li><a href="#urldomaintoasciidomain"><code>url.domainToASCII(domain)</code></a></li>
<li><a href="#urldomaintounicodedomain"><code>url.domainToUnicode(domain)</code></a></li>
<li><a href="#urlfileurltopathurl-options"><code>url.fileURLToPath(url[, options])</code></a></li>
<li><a href="#urlformaturl-options"><code>url.format(URL[, options])</code></a></li>
<li><a href="#urlpathtofileurlpath-options"><code>url.pathToFileURL(path[, options])</code></a></li>
<li><a href="#urlurltohttpoptionsurl"><code>url.urlToHttpOptions(url)</code></a></li>
</ul>
</li>
<li><span class="stability_3"><a href="#legacy-url-api">Legacy URL API</a></span>
<ul>
<li><span class="stability_3"><a href="#legacy-urlobject">Legacy <code>urlObject</code></a></span>
<ul>
<li><a href="#urlobjectauth"><code>urlObject.auth</code></a></li>
<li><a href="#urlobjecthash"><code>urlObject.hash</code></a></li>
<li><a href="#urlobjecthost"><code>urlObject.host</code></a></li>
<li><a href="#urlobjecthostname"><code>urlObject.hostname</code></a></li>
<li><a href="#urlobjecthref"><code>urlObject.href</code></a></li>
<li><a href="#urlobjectpath"><code>urlObject.path</code></a></li>
<li><a href="#urlobjectpathname"><code>urlObject.pathname</code></a></li>
<li><a href="#urlobjectport"><code>urlObject.port</code></a></li>
<li><a href="#urlobjectprotocol"><code>urlObject.protocol</code></a></li>
<li><a href="#urlobjectquery"><code>urlObject.query</code></a></li>
<li><a href="#urlobjectsearch"><code>urlObject.search</code></a></li>
<li><a href="#urlobjectslashes"><code>urlObject.slashes</code></a></li>
</ul>
</li>
<li><span class="stability_3"><a href="#urlformaturlobject"><code>url.format(urlObject)</code></a></span></li>
<li><span class="stability_0"><a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse(urlString[, parseQueryString[, slashesDenoteHost]])</code></a></span></li>
<li><span class="stability_3"><a href="#urlresolvefrom-to"><code>url.resolve(from, to)</code></a></span></li>
</ul>
</li>
<li><a href="#percent-encoding-in-urls">Percent-encoding in URLs</a>
<ul>
<li><a href="#legacy-api">Legacy API</a></li>
<li><a href="#whatwg-api">WHATWG API</a></li>
</ul>
</li>
</ul>
</li>
</ul></details>
<div role="main" id="apicontent">
<h2>URL<span><a class="mark" href="#url" id="url">#</a></span><a aria-hidden="true" class="legacy" id="url_url"></a></h2>
<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v22.14.0/lib/url.js">lib/url.js</a></p>
<p>The <code>node:url</code> module provides utilities for URL resolution and parsing. It can
be accessed using:</p>
<pre class="with-32-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> url <span class="hljs-keyword">from</span> <span class="hljs-string">'node:url'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>);</code><button class="copy-button">copy</button></pre>
<section><h3>URL strings and URL objects<span><a class="mark" href="#url-strings-and-url-objects" id="url-strings-and-url-objects">#</a></span><a aria-hidden="true" class="legacy" id="url_url_strings_and_url_objects"></a></h3>
<p>A URL string is a structured string containing multiple meaningful components.
When parsed, a URL object is returned containing properties for each of these
components.</p>
<p>The <code>node:url</code> module provides two APIs for working with URLs: a legacy API that
is Node.js specific, and a newer API that implements the same
<a href="https://url.spec.whatwg.org/">WHATWG URL Standard</a> used by web browsers.</p>
<p>A comparison between the WHATWG and legacy APIs is provided below. Above the URL
<code>'https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'</code>, properties
of an object returned by the legacy <code>url.parse()</code> are shown. Below it are
properties of a WHATWG <code>URL</code> object.</p>
<p>WHATWG URL's <code>origin</code> property includes <code>protocol</code> and <code>host</code>, but not
<code>username</code> or <code>password</code>.</p>
<pre><code class="language-text">┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ href │
├──────────┬──┬─────────────────────┬────────────────────────┬───────────────────────────┬───────┤
│ protocol │ │ auth │ host │ path │ hash │
│ │ │ ├─────────────────┬──────┼──────────┬────────────────┤ │
│ │ │ │ hostname │ port │ pathname │ search │ │
│ │ │ │ │ │ ├─┬──────────────┤ │
│ │ │ │ │ │ │ │ query │ │
" https: // user : pass @ sub.example.com : 8080 /p/a/t/h ? query=string #hash "
│ │ │ │ │ hostname │ port │ │ │ │
│ │ │ │ ├─────────────────┴──────┤ │ │ │
│ protocol │ │ username │ password │ host │ │ │ │
├──────────┴──┼──────────┴──────────┼────────────────────────┤ │ │ │
│ origin │ │ origin │ pathname │ search │ hash │
├─────────────┴─────────────────────┴────────────────────────┴──────────┴────────────────┴───────┤
│ href │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
(All spaces in the "" line should be ignored. They are purely for formatting.)</code> <button class="copy-button">copy</button></pre>
<p>Parsing the URL string using the WHATWG API:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL =
<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'</span>);</code> <button class="copy-button">copy</button></pre>
<p>Parsing the URL string using the legacy API:</p>
<pre class="with-32-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> url <span class="hljs-keyword">from</span> <span class="hljs-string">'node:url'</span>;
<span class="hljs-keyword">const</span> myURL =
url.<span class="hljs-title function_">parse</span>(<span class="hljs-string">'https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>);
<span class="hljs-keyword">const</span> myURL =
url.<span class="hljs-title function_">parse</span>(<span class="hljs-string">'https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'</span>);</code><button class="copy-button">copy</button></pre>
<h4>Constructing a URL from component parts and getting the constructed string<span><a class="mark" href="#constructing-a-url-from-component-parts-and-getting-the-constructed-string" id="constructing-a-url-from-component-parts-and-getting-the-constructed-string">#</a></span><a aria-hidden="true" class="legacy" id="url_constructing_a_url_from_component_parts_and_getting_the_constructed_string"></a></h4>
<p>It is possible to construct a WHATWG URL from component parts using either the
property setters or a template literal string:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org'</span>);
myURL.<span class="hljs-property">pathname</span> = <span class="hljs-string">'/a/b/c'</span>;
myURL.<span class="hljs-property">search</span> = <span class="hljs-string">'?d=e'</span>;
myURL.<span class="hljs-property">hash</span> = <span class="hljs-string">'#fgh'</span>;</code> <button class="copy-button">copy</button></pre>
<pre><code class="language-js"><span class="hljs-keyword">const</span> pathname = <span class="hljs-string">'/a/b/c'</span>;
<span class="hljs-keyword">const</span> search = <span class="hljs-string">'?d=e'</span>;
<span class="hljs-keyword">const</span> hash = <span class="hljs-string">'#fgh'</span>;
<span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">`https://example.org<span class="hljs-subst">${pathname}</span><span class="hljs-subst">${search}</span><span class="hljs-subst">${hash}</span>`</span>);</code> <button class="copy-button">copy</button></pre>
<p>To get the constructed URL string, use the <code>href</code> property accessor:</p>
<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);</code> <button class="copy-button">copy</button></pre>
</section><section><h3>The WHATWG URL API<span><a class="mark" href="#the-whatwg-url-api" id="the-whatwg-url-api">#</a></span><a aria-hidden="true" class="legacy" id="url_the_whatwg_url_api"></a></h3>
<h4>Class: <code>URL</code><span><a class="mark" href="#class-url" id="class-url">#</a></span><a aria-hidden="true" class="legacy" id="url_class_url"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.0.0</td>
<td><p>The class is now available on the global object.</p></td></tr>
<tr><td>v7.0.0, v6.13.0</td>
<td><p><span>Added in: v7.0.0, v6.13.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<p>Browser-compatible <code>URL</code> class, implemented by following the WHATWG URL
Standard. <a href="https://url.spec.whatwg.org/#example-url-parsing">Examples of parsed URLs</a> may be found in the Standard itself.
The <code>URL</code> class is also available on the global object.</p>
<p>In accordance with browser conventions, all properties of <code>URL</code> objects
are implemented as getters and setters on the class prototype, rather than as
data properties on the object itself. Thus, unlike <a href="#legacy-urlobject">legacy <code>urlObject</code></a>s,
using the <code>delete</code> keyword on any properties of <code>URL</code> objects (e.g. <code>delete myURL.protocol</code>, <code>delete myURL.pathname</code>, etc) has no effect but will still
return <code>true</code>.</p>
<h5><code>new URL(input[, base])</code><span><a class="mark" href="#new-urlinput-base" id="new-urlinput-base">#</a></span><a aria-hidden="true" class="legacy" id="url_new_url_input_base"></a></h5>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v20.0.0, v18.17.0</td>
<td><p>ICU requirement is removed.</p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The absolute or relative input URL to parse. If <code>input</code>
is relative, then <code>base</code> is required. If <code>input</code> is absolute, the <code>base</code>
is ignored. If <code>input</code> is not a string, it is <a href="https://tc39.es/ecma262/#sec-tostring">converted to a string</a> first.</li>
<li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The base URL to resolve against if the <code>input</code> is not
absolute. If <code>base</code> is not a string, it is <a href="https://tc39.es/ecma262/#sec-tostring">converted to a string</a> first.</li>
</ul>
<p>Creates a new <code>URL</code> object by parsing the <code>input</code> relative to the <code>base</code>. If
<code>base</code> is passed as a string, it will be parsed equivalent to <code>new URL(base)</code>.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/foo'</span>, <span class="hljs-string">'https://example.org/'</span>);
<span class="hljs-comment">// https://example.org/foo</span></code> <button class="copy-button">copy</button></pre>
<p>The URL constructor is accessible as a property on the global object.
It can also be imported from the built-in url module:</p>
<pre class="with-63-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-variable constant_">URL</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:url'</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable constant_">URL</span> === globalThis.<span class="hljs-property">URL</span>); <span class="hljs-comment">// Prints 'true'.</span></code><code class="language-js cjs"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable constant_">URL</span> === <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>).<span class="hljs-property">URL</span>); <span class="hljs-comment">// Prints 'true'.</span></code><button class="copy-button">copy</button></pre>
<p>A <code>TypeError</code> will be thrown if the <code>input</code> or <code>base</code> are not valid URLs. Note
that an effort will be made to coerce the given values into strings. For
instance:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>({ <span class="hljs-attr">toString</span>: <span class="hljs-function">() =></span> <span class="hljs-string">'https://example.org/'</span> });
<span class="hljs-comment">// https://example.org/</span></code> <button class="copy-button">copy</button></pre>
<p>Unicode characters appearing within the host name of <code>input</code> will be
automatically converted to ASCII using the <a href="https://tools.ietf.org/html/rfc5891#section-4.4">Punycode</a> algorithm.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://測試'</span>);
<span class="hljs-comment">// https://xn--g6w251d/</span></code> <button class="copy-button">copy</button></pre>
<p>In cases where it is not known in advance if <code>input</code> is an absolute URL
and a <code>base</code> is provided, it is advised to validate that the <code>origin</code> of
the <code>URL</code> object is what is expected.</p>
<pre><code class="language-js"><span class="hljs-keyword">let</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'http://Example.com/'</span>, <span class="hljs-string">'https://example.org/'</span>);
<span class="hljs-comment">// http://example.com/</span>
myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://Example.com/'</span>, <span class="hljs-string">'https://example.org/'</span>);
<span class="hljs-comment">// https://example.com/</span>
myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'foo://Example.com/'</span>, <span class="hljs-string">'https://example.org/'</span>);
<span class="hljs-comment">// foo://Example.com/</span>
myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'http:Example.com/'</span>, <span class="hljs-string">'https://example.org/'</span>);
<span class="hljs-comment">// http://example.com/</span>
myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https:Example.com/'</span>, <span class="hljs-string">'https://example.org/'</span>);
<span class="hljs-comment">// https://example.org/Example.com/</span>
myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'foo:Example.com/'</span>, <span class="hljs-string">'https://example.org/'</span>);
<span class="hljs-comment">// foo:Example.com/</span></code> <button class="copy-button">copy</button></pre>
<h5><code>url.hash</code><span><a class="mark" href="#urlhash" id="urlhash">#</a></span><a aria-hidden="true" class="legacy" id="url_url_hash"></a></h5>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Gets and sets the fragment portion of the URL.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org/foo#bar'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">hash</span>);
<span class="hljs-comment">// Prints #bar</span>
myURL.<span class="hljs-property">hash</span> = <span class="hljs-string">'baz'</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
<span class="hljs-comment">// Prints https://example.org/foo#baz</span></code> <button class="copy-button">copy</button></pre>
<p>Invalid URL characters included in the value assigned to the <code>hash</code> property
are <a href="#percent-encoding-in-urls">percent-encoded</a>. The selection of which characters to
percent-encode may vary somewhat from what the <a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse()</code></a> and
<a href="#urlformaturlobject"><code>url.format()</code></a> methods would produce.</p>
<h5><code>url.host</code><span><a class="mark" href="#urlhost" id="urlhost">#</a></span><a aria-hidden="true" class="legacy" id="url_url_host"></a></h5>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Gets and sets the host portion of the URL.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org:81/foo'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">host</span>);
<span class="hljs-comment">// Prints example.org:81</span>
myURL.<span class="hljs-property">host</span> = <span class="hljs-string">'example.com:82'</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
<span class="hljs-comment">// Prints https://example.com:82/foo</span></code> <button class="copy-button">copy</button></pre>
<p>Invalid host values assigned to the <code>host</code> property are ignored.</p>
<h5><code>url.hostname</code><span><a class="mark" href="#urlhostname" id="urlhostname">#</a></span><a aria-hidden="true" class="legacy" id="url_url_hostname"></a></h5>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Gets and sets the host name portion of the URL. The key difference between
<code>url.host</code> and <code>url.hostname</code> is that <code>url.hostname</code> does <em>not</em> include the
port.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org:81/foo'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">hostname</span>);
<span class="hljs-comment">// Prints example.org</span>
<span class="hljs-comment">// Setting the hostname does not change the port</span>
myURL.<span class="hljs-property">hostname</span> = <span class="hljs-string">'example.com'</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
<span class="hljs-comment">// Prints https://example.com:81/foo</span>
<span class="hljs-comment">// Use myURL.host to change the hostname and port</span>
myURL.<span class="hljs-property">host</span> = <span class="hljs-string">'example.org:82'</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
<span class="hljs-comment">// Prints https://example.org:82/foo</span></code> <button class="copy-button">copy</button></pre>
<p>Invalid host name values assigned to the <code>hostname</code> property are ignored.</p>
<h5><code>url.href</code><span><a class="mark" href="#urlhref" id="urlhref">#</a></span><a aria-hidden="true" class="legacy" id="url_url_href"></a></h5>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Gets and sets the serialized URL.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org/foo'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
<span class="hljs-comment">// Prints https://example.org/foo</span>
myURL.<span class="hljs-property">href</span> = <span class="hljs-string">'https://example.com/bar'</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
<span class="hljs-comment">// Prints https://example.com/bar</span></code> <button class="copy-button">copy</button></pre>
<p>Getting the value of the <code>href</code> property is equivalent to calling
<a href="#urltostring"><code>url.toString()</code></a>.</p>
<p>Setting the value of this property to a new value is equivalent to creating a
new <code>URL</code> object using <a href="#new-urlinput-base"><code>new URL(value)</code></a>. Each of the <code>URL</code>
object's properties will be modified.</p>
<p>If the value assigned to the <code>href</code> property is not a valid URL, a <code>TypeError</code>
will be thrown.</p>
<h5><code>url.origin</code><span><a class="mark" href="#urlorigin" id="urlorigin">#</a></span><a aria-hidden="true" class="legacy" id="url_url_origin"></a></h5>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v15.0.0</td>
<td><p>The scheme "gopher" is no longer special and <code>url.origin</code> now returns <code>'null'</code> for it.</p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Gets the read-only serialization of the URL's origin.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org/foo/bar?baz'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">origin</span>);
<span class="hljs-comment">// Prints https://example.org</span></code> <button class="copy-button">copy</button></pre>
<pre><code class="language-js"><span class="hljs-keyword">const</span> idnURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://測試'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(idnURL.<span class="hljs-property">origin</span>);
<span class="hljs-comment">// Prints https://xn--g6w251d</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(idnURL.<span class="hljs-property">hostname</span>);
<span class="hljs-comment">// Prints xn--g6w251d</span></code> <button class="copy-button">copy</button></pre>
<h5><code>url.password</code><span><a class="mark" href="#urlpassword" id="urlpassword">#</a></span><a aria-hidden="true" class="legacy" id="url_url_password"></a></h5>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Gets and sets the password portion of the URL.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://abc:xyz@example.com'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">password</span>);
<span class="hljs-comment">// Prints xyz</span>
myURL.<span class="hljs-property">password</span> = <span class="hljs-string">'123'</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
<span class="hljs-comment">// Prints https://abc:123@example.com/</span></code> <button class="copy-button">copy</button></pre>
<p>Invalid URL characters included in the value assigned to the <code>password</code> property
are <a href="#percent-encoding-in-urls">percent-encoded</a>. The selection of which characters to
percent-encode may vary somewhat from what the <a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse()</code></a> and
<a href="#urlformaturlobject"><code>url.format()</code></a> methods would produce.</p>
<h5><code>url.pathname</code><span><a class="mark" href="#urlpathname" id="urlpathname">#</a></span><a aria-hidden="true" class="legacy" id="url_url_pathname"></a></h5>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Gets and sets the path portion of the URL.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org/abc/xyz?123'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">pathname</span>);
<span class="hljs-comment">// Prints /abc/xyz</span>
myURL.<span class="hljs-property">pathname</span> = <span class="hljs-string">'/abcdef'</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
<span class="hljs-comment">// Prints https://example.org/abcdef?123</span></code> <button class="copy-button">copy</button></pre>
<p>Invalid URL characters included in the value assigned to the <code>pathname</code>
property are <a href="#percent-encoding-in-urls">percent-encoded</a>. The selection of which characters
to percent-encode may vary somewhat from what the <a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse()</code></a> and
<a href="#urlformaturlobject"><code>url.format()</code></a> methods would produce.</p>
<h5><code>url.port</code><span><a class="mark" href="#urlport" id="urlport">#</a></span><a aria-hidden="true" class="legacy" id="url_url_port"></a></h5>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v15.0.0</td>
<td><p>The scheme "gopher" is no longer special.</p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Gets and sets the port portion of the URL.</p>
<p>The port value may be a number or a string containing a number in the range
<code>0</code> to <code>65535</code> (inclusive). Setting the value to the default port of the
<code>URL</code> objects given <code>protocol</code> will result in the <code>port</code> value becoming
the empty string (<code>''</code>).</p>
<p>The port value can be an empty string in which case the port depends on
the protocol/scheme:</p>
<table><thead><tr><th>protocol</th><th>port</th></tr></thead><tbody><tr><td>"ftp"</td><td>21</td></tr><tr><td>"file"</td><td></td></tr><tr><td>"http"</td><td>80</td></tr><tr><td>"https"</td><td>443</td></tr><tr><td>"ws"</td><td>80</td></tr><tr><td>"wss"</td><td>443</td></tr></tbody></table>
<p>Upon assigning a value to the port, the value will first be converted to a
string using <code>.toString()</code>.</p>
<p>If that string is invalid but it begins with a number, the leading number is
assigned to <code>port</code>.
If the number lies outside the range denoted above, it is ignored.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org:8888'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
<span class="hljs-comment">// Prints 8888</span>
<span class="hljs-comment">// Default ports are automatically transformed to the empty string</span>
<span class="hljs-comment">// (HTTPS protocol's default port is 443)</span>
myURL.<span class="hljs-property">port</span> = <span class="hljs-string">'443'</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
<span class="hljs-comment">// Prints the empty string</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
<span class="hljs-comment">// Prints https://example.org/</span>
myURL.<span class="hljs-property">port</span> = <span class="hljs-number">1234</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
<span class="hljs-comment">// Prints 1234</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
<span class="hljs-comment">// Prints https://example.org:1234/</span>
<span class="hljs-comment">// Completely invalid port strings are ignored</span>
myURL.<span class="hljs-property">port</span> = <span class="hljs-string">'abcd'</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
<span class="hljs-comment">// Prints 1234</span>
<span class="hljs-comment">// Leading numbers are treated as a port number</span>
myURL.<span class="hljs-property">port</span> = <span class="hljs-string">'5678abcd'</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
<span class="hljs-comment">// Prints 5678</span>
<span class="hljs-comment">// Non-integers are truncated</span>
myURL.<span class="hljs-property">port</span> = <span class="hljs-number">1234.5678</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
<span class="hljs-comment">// Prints 1234</span>
<span class="hljs-comment">// Out-of-range numbers which are not represented in scientific notation</span>
<span class="hljs-comment">// will be ignored.</span>
myURL.<span class="hljs-property">port</span> = <span class="hljs-number">1e10</span>; <span class="hljs-comment">// 10000000000, will be range-checked as described below</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
<span class="hljs-comment">// Prints 1234</span></code> <button class="copy-button">copy</button></pre>
<p>Numbers which contain a decimal point,
such as floating-point numbers or numbers in scientific notation,
are not an exception to this rule.
Leading numbers up to the decimal point will be set as the URL's port,
assuming they are valid:</p>
<pre><code class="language-js">myURL.<span class="hljs-property">port</span> = <span class="hljs-number">4.567e21</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">port</span>);
<span class="hljs-comment">// Prints 4 (because it is the leading number in the string '4.567e21')</span></code> <button class="copy-button">copy</button></pre>
<h5><code>url.protocol</code><span><a class="mark" href="#urlprotocol" id="urlprotocol">#</a></span><a aria-hidden="true" class="legacy" id="url_url_protocol"></a></h5>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Gets and sets the protocol portion of the URL.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">protocol</span>);
<span class="hljs-comment">// Prints https:</span>
myURL.<span class="hljs-property">protocol</span> = <span class="hljs-string">'ftp'</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
<span class="hljs-comment">// Prints ftp://example.org/</span></code> <button class="copy-button">copy</button></pre>
<p>Invalid URL protocol values assigned to the <code>protocol</code> property are ignored.</p>
<h6>Special schemes<span><a class="mark" href="#special-schemes" id="special-schemes">#</a></span><a aria-hidden="true" class="legacy" id="url_special_schemes"></a></h6>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v15.0.0</td>
<td><p>The scheme "gopher" is no longer special.</p></td></tr>
</tbody></table>
</details>
</div>
<p>The <a href="https://url.spec.whatwg.org/">WHATWG URL Standard</a> considers a handful of URL protocol schemes to be
<em>special</em> in terms of how they are parsed and serialized. When a URL is
parsed using one of these special protocols, the <code>url.protocol</code> property
may be changed to another special protocol but cannot be changed to a
non-special protocol, and vice versa.</p>
<p>For instance, changing from <code>http</code> to <code>https</code> works:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> u = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'http://example.org'</span>);
u.<span class="hljs-property">protocol</span> = <span class="hljs-string">'https'</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(u.<span class="hljs-property">href</span>);
<span class="hljs-comment">// https://example.org/</span></code> <button class="copy-button">copy</button></pre>
<p>However, changing from <code>http</code> to a hypothetical <code>fish</code> protocol does not
because the new protocol is not special.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> u = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'http://example.org'</span>);
u.<span class="hljs-property">protocol</span> = <span class="hljs-string">'fish'</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(u.<span class="hljs-property">href</span>);
<span class="hljs-comment">// http://example.org/</span></code> <button class="copy-button">copy</button></pre>
<p>Likewise, changing from a non-special protocol to a special protocol is also
not permitted:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> u = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'fish://example.org'</span>);
u.<span class="hljs-property">protocol</span> = <span class="hljs-string">'http'</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(u.<span class="hljs-property">href</span>);
<span class="hljs-comment">// fish://example.org</span></code> <button class="copy-button">copy</button></pre>
<p>According to the WHATWG URL Standard, special protocol schemes are <code>ftp</code>,
<code>file</code>, <code>http</code>, <code>https</code>, <code>ws</code>, and <code>wss</code>.</p>
<h5><code>url.search</code><span><a class="mark" href="#urlsearch" id="urlsearch">#</a></span><a aria-hidden="true" class="legacy" id="url_url_search"></a></h5>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Gets and sets the serialized query portion of the URL.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org/abc?123'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">search</span>);
<span class="hljs-comment">// Prints ?123</span>
myURL.<span class="hljs-property">search</span> = <span class="hljs-string">'abc=xyz'</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
<span class="hljs-comment">// Prints https://example.org/abc?abc=xyz</span></code> <button class="copy-button">copy</button></pre>
<p>Any invalid URL characters appearing in the value assigned the <code>search</code>
property will be <a href="#percent-encoding-in-urls">percent-encoded</a>. The selection of which
characters to percent-encode may vary somewhat from what the <a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse()</code></a>
and <a href="#urlformaturlobject"><code>url.format()</code></a> methods would produce.</p>
<h5><code>url.searchParams</code><span><a class="mark" href="#urlsearchparams" id="urlsearchparams">#</a></span><a aria-hidden="true" class="legacy" id="url_url_searchparams"></a></h5>
<ul>
<li><a href="url.html#class-urlsearchparams" class="type"><URLSearchParams></a></li>
</ul>
<p>Gets the <a href="#class-urlsearchparams"><code>URLSearchParams</code></a> object representing the query parameters of the
URL. This property is read-only but the <code>URLSearchParams</code> object it provides
can be used to mutate the URL instance; to replace the entirety of query
parameters of the URL, use the <a href="#urlsearch"><code>url.search</code></a> setter. See
<a href="#class-urlsearchparams"><code>URLSearchParams</code></a> documentation for details.</p>
<p>Use care when using <code>.searchParams</code> to modify the <code>URL</code> because,
per the WHATWG specification, the <code>URLSearchParams</code> object uses
different rules to determine which characters to percent-encode. For
instance, the <code>URL</code> object will not percent encode the ASCII tilde (<code>~</code>)
character, while <code>URLSearchParams</code> will always encode it:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org/abc?foo=~bar'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">search</span>); <span class="hljs-comment">// prints ?foo=~bar</span>
<span class="hljs-comment">// Modify the URL via searchParams...</span>
myURL.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">sort</span>();
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">search</span>); <span class="hljs-comment">// prints ?foo=%7Ebar</span></code> <button class="copy-button">copy</button></pre>
<h5><code>url.username</code><span><a class="mark" href="#urlusername" id="urlusername">#</a></span><a aria-hidden="true" class="legacy" id="url_url_username"></a></h5>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Gets and sets the username portion of the URL.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://abc:xyz@example.com'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">username</span>);
<span class="hljs-comment">// Prints abc</span>
myURL.<span class="hljs-property">username</span> = <span class="hljs-string">'123'</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
<span class="hljs-comment">// Prints https://123:xyz@example.com/</span></code> <button class="copy-button">copy</button></pre>
<p>Any invalid URL characters appearing in the value assigned the <code>username</code>
property will be <a href="#percent-encoding-in-urls">percent-encoded</a>. The selection of which
characters to percent-encode may vary somewhat from what the <a href="#urlparseurlstring-parsequerystring-slashesdenotehost"><code>url.parse()</code></a>
and <a href="#urlformaturlobject"><code>url.format()</code></a> methods would produce.</p>
<h5><code>url.toString()</code><span><a class="mark" href="#urltostring" id="urltostring">#</a></span><a aria-hidden="true" class="legacy" id="url_url_tostring"></a></h5>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>The <code>toString()</code> method on the <code>URL</code> object returns the serialized URL. The
value returned is equivalent to that of <a href="#urlhref"><code>url.href</code></a> and <a href="#urltojson"><code>url.toJSON()</code></a>.</p>
<h5><code>url.toJSON()</code><span><a class="mark" href="#urltojson" id="urltojson">#</a></span><a aria-hidden="true" class="legacy" id="url_url_tojson"></a></h5>
<div class="api_metadata">
<span>Added in: v7.7.0, v6.13.0</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>The <code>toJSON()</code> method on the <code>URL</code> object returns the serialized URL. The
value returned is equivalent to that of <a href="#urlhref"><code>url.href</code></a> and
<a href="#urltostring"><code>url.toString()</code></a>.</p>
<p>This method is automatically called when an <code>URL</code> object is serialized
with <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify"><code>JSON.stringify()</code></a>.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> myURLs = [
<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://www.example.com'</span>),
<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://test.example.org'</span>),
];
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(myURLs));
<span class="hljs-comment">// Prints ["https://www.example.com/","https://test.example.org/"]</span></code> <button class="copy-button">copy</button></pre>
<h5><code>URL.createObjectURL(blob)</code><span><a class="mark" href="#urlcreateobjecturlblob" id="urlcreateobjecturlblob">#</a></span><a aria-hidden="true" class="legacy" id="url_url_createobjecturl_blob"></a></h5>
<div class="api_metadata">
<span>Added in: v16.7.0</span>
</div>
<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
<ul>
<li><code>blob</code> <a href="buffer.html#class-blob" class="type"><Blob></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Creates a <code>'blob:nodedata:...'</code> URL string that represents the given <a href="buffer.html#class-blob" class="type"><Blob></a>
object and can be used to retrieve the <code>Blob</code> later.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> {
<span class="hljs-title class_">Blob</span>,
resolveObjectURL,
} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
<span class="hljs-keyword">const</span> blob = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Blob</span>([<span class="hljs-string">'hello'</span>]);
<span class="hljs-keyword">const</span> id = <span class="hljs-variable constant_">URL</span>.<span class="hljs-title function_">createObjectURL</span>(blob);
<span class="hljs-comment">// later...</span>
<span class="hljs-keyword">const</span> otherBlob = <span class="hljs-title function_">resolveObjectURL</span>(id);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(otherBlob.<span class="hljs-property">size</span>);</code> <button class="copy-button">copy</button></pre>
<p>The data stored by the registered <a href="buffer.html#class-blob" class="type"><Blob></a> will be retained in memory until
<code>URL.revokeObjectURL()</code> is called to remove it.</p>
<p><code>Blob</code> objects are registered within the current thread. If using Worker
Threads, <code>Blob</code> objects registered within one Worker will not be available
to other workers or the main thread.</p>
<h5><code>URL.revokeObjectURL(id)</code><span><a class="mark" href="#urlrevokeobjecturlid" id="urlrevokeobjecturlid">#</a></span><a aria-hidden="true" class="legacy" id="url_url_revokeobjecturl_id"></a></h5>
<div class="api_metadata">
<span>Added in: v16.7.0</span>
</div>
<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
<ul>
<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A <code>'blob:nodedata:...</code> URL string returned by a prior call to
<code>URL.createObjectURL()</code>.</li>
</ul>
<p>Removes the stored <a href="buffer.html#class-blob" class="type"><Blob></a> identified by the given ID. Attempting to revoke a
ID that isn't registered will silently fail.</p>
<h5><code>URL.canParse(input[, base])</code><span><a class="mark" href="#urlcanparseinput-base" id="urlcanparseinput-base">#</a></span><a aria-hidden="true" class="legacy" id="url_url_canparse_input_base"></a></h5>
<div class="api_metadata">
<span>Added in: v19.9.0, v18.17.0</span>
</div>
<ul>
<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The absolute or relative input URL to parse. If <code>input</code>
is relative, then <code>base</code> is required. If <code>input</code> is absolute, the <code>base</code>
is ignored. If <code>input</code> is not a string, it is <a href="https://tc39.es/ecma262/#sec-tostring">converted to a string</a> first.</li>
<li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The base URL to resolve against if the <code>input</code> is not
absolute. If <code>base</code> is not a string, it is <a href="https://tc39.es/ecma262/#sec-tostring">converted to a string</a> first.</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Checks if an <code>input</code> relative to the <code>base</code> can be parsed to a <code>URL</code>.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> isValid = <span class="hljs-variable constant_">URL</span>.<span class="hljs-title function_">canParse</span>(<span class="hljs-string">'/foo'</span>, <span class="hljs-string">'https://example.org/'</span>); <span class="hljs-comment">// true</span>
<span class="hljs-keyword">const</span> isNotValid = <span class="hljs-variable constant_">URL</span>.<span class="hljs-title function_">canParse</span>(<span class="hljs-string">'/foo'</span>); <span class="hljs-comment">// false</span></code> <button class="copy-button">copy</button></pre>
<h5><code>URL.parse(input[, base])</code><span><a class="mark" href="#urlparseinput-base" id="urlparseinput-base">#</a></span><a aria-hidden="true" class="legacy" id="url_url_parse_input_base"></a></h5>
<div class="api_metadata">
<span>Added in: v22.1.0</span>
</div>
<ul>
<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The absolute or relative input URL to parse. If <code>input</code>
is relative, then <code>base</code> is required. If <code>input</code> is absolute, the <code>base</code>
is ignored. If <code>input</code> is not a string, it is <a href="https://tc39.es/ecma262/#sec-tostring">converted to a string</a> first.</li>
<li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The base URL to resolve against if the <code>input</code> is not
absolute. If <code>base</code> is not a string, it is <a href="https://tc39.es/ecma262/#sec-tostring">converted to a string</a> first.</li>
<li>Returns: <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a></li>
</ul>
<p>Parses a string as a URL. If <code>base</code> is provided, it will be used as the base
URL for the purpose of resolving non-absolute <code>input</code> URLs. Returns <code>null</code>
if <code>input</code> is not a valid.</p>
<h4>Class: <code>URLSearchParams</code><span><a class="mark" href="#class-urlsearchparams" id="class-urlsearchparams">#</a></span><a aria-hidden="true" class="legacy" id="url_class_urlsearchparams"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.0.0</td>
<td><p>The class is now available on the global object.</p></td></tr>
<tr><td>v7.5.0, v6.13.0</td>
<td><p><span>Added in: v7.5.0, v6.13.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<p>The <code>URLSearchParams</code> API provides read and write access to the query of a
<code>URL</code>. The <code>URLSearchParams</code> class can also be used standalone with one of the
four following constructors.
The <code>URLSearchParams</code> class is also available on the global object.</p>
<p>The WHATWG <code>URLSearchParams</code> interface and the <a href="querystring.html"><code>querystring</code></a> module have
similar purpose, but the purpose of the <a href="querystring.html"><code>querystring</code></a> module is more
general, as it allows the customization of delimiter characters (<code>&</code> and <code>=</code>).
On the other hand, this API is designed purely for URL query strings.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org/?abc=123'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">get</span>(<span class="hljs-string">'abc'</span>));
<span class="hljs-comment">// Prints 123</span>
myURL.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">append</span>(<span class="hljs-string">'abc'</span>, <span class="hljs-string">'xyz'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
<span class="hljs-comment">// Prints https://example.org/?abc=123&abc=xyz</span>
myURL.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">delete</span>(<span class="hljs-string">'abc'</span>);
myURL.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">set</span>(<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
<span class="hljs-comment">// Prints https://example.org/?a=b</span>
<span class="hljs-keyword">const</span> newSearchParams = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(myURL.<span class="hljs-property">searchParams</span>);
<span class="hljs-comment">// The above is equivalent to</span>
<span class="hljs-comment">// const newSearchParams = new URLSearchParams(myURL.search);</span>
newSearchParams.<span class="hljs-title function_">append</span>(<span class="hljs-string">'a'</span>, <span class="hljs-string">'c'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
<span class="hljs-comment">// Prints https://example.org/?a=b</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(newSearchParams.<span class="hljs-title function_">toString</span>());
<span class="hljs-comment">// Prints a=b&a=c</span>
<span class="hljs-comment">// newSearchParams.toString() is implicitly called</span>
myURL.<span class="hljs-property">search</span> = newSearchParams;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
<span class="hljs-comment">// Prints https://example.org/?a=b&a=c</span>
newSearchParams.<span class="hljs-title function_">delete</span>(<span class="hljs-string">'a'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
<span class="hljs-comment">// Prints https://example.org/?a=b&a=c</span></code> <button class="copy-button">copy</button></pre>
<h5><code>new URLSearchParams()</code><span><a class="mark" href="#new-urlsearchparams" id="new-urlsearchparams">#</a></span><a aria-hidden="true" class="legacy" id="url_new_urlsearchparams"></a></h5>
<p>Instantiate a new empty <code>URLSearchParams</code> object.</p>
<h5><code>new URLSearchParams(string)</code><span><a class="mark" href="#new-urlsearchparamsstring" id="new-urlsearchparamsstring">#</a></span><a aria-hidden="true" class="legacy" id="url_new_urlsearchparams_string"></a></h5>
<ul>
<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A query string</li>
</ul>
<p>Parse the <code>string</code> as a query string, and use it to instantiate a new
<code>URLSearchParams</code> object. A leading <code>'?'</code>, if present, is ignored.</p>
<pre><code class="language-js"><span class="hljs-keyword">let</span> params;
params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(<span class="hljs-string">'user=abc&query=xyz'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">get</span>(<span class="hljs-string">'user'</span>));
<span class="hljs-comment">// Prints 'abc'</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
<span class="hljs-comment">// Prints 'user=abc&query=xyz'</span>
params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(<span class="hljs-string">'?user=abc&query=xyz'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
<span class="hljs-comment">// Prints 'user=abc&query=xyz'</span></code> <button class="copy-button">copy</button></pre>
<h5><code>new URLSearchParams(obj)</code><span><a class="mark" href="#new-urlsearchparamsobj" id="new-urlsearchparamsobj">#</a></span><a aria-hidden="true" class="legacy" id="url_new_urlsearchparams_obj"></a></h5>
<div class="api_metadata">
<span>Added in: v7.10.0, v6.13.0</span>
</div>
<ul>
<li><code>obj</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> An object representing a collection of key-value pairs</li>
</ul>
<p>Instantiate a new <code>URLSearchParams</code> object with a query hash map. The key and
value of each property of <code>obj</code> are always coerced to strings.</p>
<p>Unlike <a href="querystring.html"><code>querystring</code></a> module, duplicate keys in the form of array values are
not allowed. Arrays are stringified using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString"><code>array.toString()</code></a>, which simply
joins all array elements with commas.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>({
<span class="hljs-attr">user</span>: <span class="hljs-string">'abc'</span>,
<span class="hljs-attr">query</span>: [<span class="hljs-string">'first'</span>, <span class="hljs-string">'second'</span>],
});
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">getAll</span>(<span class="hljs-string">'query'</span>));
<span class="hljs-comment">// Prints [ 'first,second' ]</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
<span class="hljs-comment">// Prints 'user=abc&query=first%2Csecond'</span></code> <button class="copy-button">copy</button></pre>
<h5><code>new URLSearchParams(iterable)</code><span><a class="mark" href="#new-urlsearchparamsiterable" id="new-urlsearchparamsiterable">#</a></span><a aria-hidden="true" class="legacy" id="url_new_urlsearchparams_iterable"></a></h5>
<div class="api_metadata">
<span>Added in: v7.10.0, v6.13.0</span>
</div>
<ul>
<li><code>iterable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type"><Iterable></a> An iterable object whose elements are key-value pairs</li>
</ul>
<p>Instantiate a new <code>URLSearchParams</code> object with an iterable map in a way that
is similar to <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"><code>Map</code></a>'s constructor. <code>iterable</code> can be an <code>Array</code> or any
iterable object. That means <code>iterable</code> can be another <code>URLSearchParams</code>, in
which case the constructor will simply create a clone of the provided
<code>URLSearchParams</code>. Elements of <code>iterable</code> are key-value pairs, and can
themselves be any iterable object.</p>
<p>Duplicate keys are allowed.</p>
<pre><code class="language-js"><span class="hljs-keyword">let</span> params;
<span class="hljs-comment">// Using an array</span>
params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>([
[<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>],
[<span class="hljs-string">'query'</span>, <span class="hljs-string">'first'</span>],
[<span class="hljs-string">'query'</span>, <span class="hljs-string">'second'</span>],
]);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
<span class="hljs-comment">// Prints 'user=abc&query=first&query=second'</span>
<span class="hljs-comment">// Using a Map object</span>
<span class="hljs-keyword">const</span> map = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>();
map.<span class="hljs-title function_">set</span>(<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>);
map.<span class="hljs-title function_">set</span>(<span class="hljs-string">'query'</span>, <span class="hljs-string">'xyz'</span>);
params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(map);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
<span class="hljs-comment">// Prints 'user=abc&query=xyz'</span>
<span class="hljs-comment">// Using a generator function</span>
<span class="hljs-keyword">function</span>* <span class="hljs-title function_">getQueryPairs</span>(<span class="hljs-params"></span>) {
<span class="hljs-keyword">yield</span> [<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>];
<span class="hljs-keyword">yield</span> [<span class="hljs-string">'query'</span>, <span class="hljs-string">'first'</span>];
<span class="hljs-keyword">yield</span> [<span class="hljs-string">'query'</span>, <span class="hljs-string">'second'</span>];
}
params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(<span class="hljs-title function_">getQueryPairs</span>());
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
<span class="hljs-comment">// Prints 'user=abc&query=first&query=second'</span>
<span class="hljs-comment">// Each key-value pair must have exactly two elements</span>
<span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>([
[<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>, <span class="hljs-string">'error'</span>],
]);
<span class="hljs-comment">// Throws TypeError [ERR_INVALID_TUPLE]:</span>
<span class="hljs-comment">// Each query pair must be an iterable [name, value] tuple</span></code> <button class="copy-button">copy</button></pre>
<h5><code>urlSearchParams.append(name, value)</code><span><a class="mark" href="#urlsearchparamsappendname-value" id="urlsearchparamsappendname-value">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_append_name_value"></a></h5>
<ul>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Append a new name-value pair to the query string.</p>
<h5><code>urlSearchParams.delete(name[, value])</code><span><a class="mark" href="#urlsearchparamsdeletename-value" id="urlsearchparamsdeletename-value">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_delete_name_value"></a></h5>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v20.2.0, v18.18.0</td>
<td><p>Add support for optional <code>value</code> argument.</p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>If <code>value</code> is provided, removes all name-value pairs
where name is <code>name</code> and value is <code>value</code>..</p>
<p>If <code>value</code> is not provided, removes all name-value pairs whose name is <code>name</code>.</p>
<h5><code>urlSearchParams.entries()</code><span><a class="mark" href="#urlsearchparamsentries" id="urlsearchparamsentries">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_entries"></a></h5>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li>
</ul>
<p>Returns an ES6 <code>Iterator</code> over each of the name-value pairs in the query.
Each item of the iterator is a JavaScript <code>Array</code>. The first item of the <code>Array</code>
is the <code>name</code>, the second item of the <code>Array</code> is the <code>value</code>.</p>
<p>Alias for <a href="#urlsearchparamssymboliterator"><code>urlSearchParams[@@iterator]()</code></a>.</p>
<h5><code>urlSearchParams.forEach(fn[, thisArg])</code><span><a class="mark" href="#urlsearchparamsforeachfn-thisarg" id="urlsearchparamsforeachfn-thisarg">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_foreach_fn_thisarg"></a></h5>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>fn</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Invoked for each name-value pair in the query</li>
<li><code>thisArg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> To be used as <code>this</code> value for when <code>fn</code> is called</li>
</ul>
<p>Iterates over each name-value pair in the query and invokes the given function.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://example.org/?a=b&c=d'</span>);
myURL.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">value, name, searchParams</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name, value, myURL.<span class="hljs-property">searchParams</span> === searchParams);
});
<span class="hljs-comment">// Prints:</span>
<span class="hljs-comment">// a b true</span>
<span class="hljs-comment">// c d true</span></code> <button class="copy-button">copy</button></pre>
<h5><code>urlSearchParams.get(name)</code><span><a class="mark" href="#urlsearchparamsgetname" id="urlsearchparamsgetname">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_get_name"></a></h5>
<ul>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> A string or <code>null</code> if there is no name-value pair
with the given <code>name</code>.</li>
</ul>
<p>Returns the value of the first name-value pair whose name is <code>name</code>. If there
are no such pairs, <code>null</code> is returned.</p>
<h5><code>urlSearchParams.getAll(name)</code><span><a class="mark" href="#urlsearchparamsgetallname" id="urlsearchparamsgetallname">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_getall_name"></a></h5>
<ul>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li>
</ul>
<p>Returns the values of all name-value pairs whose name is <code>name</code>. If there are
no such pairs, an empty array is returned.</p>
<h5><code>urlSearchParams.has(name[, value])</code><span><a class="mark" href="#urlsearchparamshasname-value" id="urlsearchparamshasname-value">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_has_name_value"></a></h5>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v20.2.0, v18.18.0</td>
<td><p>Add support for optional <code>value</code> argument.</p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Checks if the <code>URLSearchParams</code> object contains key-value pair(s) based on
<code>name</code> and an optional <code>value</code> argument.</p>
<p>If <code>value</code> is provided, returns <code>true</code> when name-value pair with
same <code>name</code> and <code>value</code> exists.</p>
<p>If <code>value</code> is not provided, returns <code>true</code> if there is at least one name-value
pair whose name is <code>name</code>.</p>
<h5><code>urlSearchParams.keys()</code><span><a class="mark" href="#urlsearchparamskeys" id="urlsearchparamskeys">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_keys"></a></h5>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li>
</ul>
<p>Returns an ES6 <code>Iterator</code> over the names of each name-value pair.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(<span class="hljs-string">'foo=bar&foo=baz'</span>);
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> name <span class="hljs-keyword">of</span> params.<span class="hljs-title function_">keys</span>()) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name);
}
<span class="hljs-comment">// Prints:</span>
<span class="hljs-comment">// foo</span>
<span class="hljs-comment">// foo</span></code> <button class="copy-button">copy</button></pre>
<h5><code>urlSearchParams.set(name, value)</code><span><a class="mark" href="#urlsearchparamssetname-value" id="urlsearchparamssetname-value">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_set_name_value"></a></h5>
<ul>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Sets the value in the <code>URLSearchParams</code> object associated with <code>name</code> to
<code>value</code>. If there are any pre-existing name-value pairs whose names are <code>name</code>,
set the first such pair's value to <code>value</code> and remove all others. If not,
append the name-value pair to the query string.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>();
params.<span class="hljs-title function_">append</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'bar'</span>);
params.<span class="hljs-title function_">append</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'baz'</span>);
params.<span class="hljs-title function_">append</span>(<span class="hljs-string">'abc'</span>, <span class="hljs-string">'def'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
<span class="hljs-comment">// Prints foo=bar&foo=baz&abc=def</span>
params.<span class="hljs-title function_">set</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'def'</span>);
params.<span class="hljs-title function_">set</span>(<span class="hljs-string">'xyz'</span>, <span class="hljs-string">'opq'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
<span class="hljs-comment">// Prints foo=def&abc=def&xyz=opq</span></code> <button class="copy-button">copy</button></pre>
<h5><code>urlSearchParams.size</code><span><a class="mark" href="#urlsearchparamssize" id="urlsearchparamssize">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_size"></a></h5>
<div class="api_metadata">
<span>Added in: v19.8.0, v18.16.0</span>
</div>
<p>The total number of parameter entries.</p>
<h5><code>urlSearchParams.sort()</code><span><a class="mark" href="#urlsearchparamssort" id="urlsearchparamssort">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_sort"></a></h5>
<div class="api_metadata">
<span>Added in: v7.7.0, v6.13.0</span>
</div>
<p>Sort all existing name-value pairs in-place by their names. Sorting is done
with a <a href="https://en.wikipedia.org/wiki/Sorting_algorithm#Stability">stable sorting algorithm</a>, so relative order between name-value pairs
with the same name is preserved.</p>
<p>This method can be used, in particular, to increase cache hits.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(<span class="hljs-string">'query[]=abc&type=search&query[]=123'</span>);
params.<span class="hljs-title function_">sort</span>();
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>());
<span class="hljs-comment">// Prints query%5B%5D=abc&query%5B%5D=123&type=search</span></code> <button class="copy-button">copy</button></pre>
<h5><code>urlSearchParams.toString()</code><span><a class="mark" href="#urlsearchparamstostring" id="urlsearchparamstostring">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_tostring"></a></h5>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Returns the search parameters serialized as a string, with characters
percent-encoded where necessary.</p>
<h5><code>urlSearchParams.values()</code><span><a class="mark" href="#urlsearchparamsvalues" id="urlsearchparamsvalues">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_values"></a></h5>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li>
</ul>
<p>Returns an ES6 <code>Iterator</code> over the values of each name-value pair.</p>
<h5><code>urlSearchParams[Symbol.iterator]()</code><span><a class="mark" href="#urlsearchparamssymboliterator" id="urlsearchparamssymboliterator">#</a></span><a aria-hidden="true" class="legacy" id="url_urlsearchparams_symbol_iterator"></a></h5>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li>
</ul>
<p>Returns an ES6 <code>Iterator</code> over each of the name-value pairs in the query string.
Each item of the iterator is a JavaScript <code>Array</code>. The first item of the <code>Array</code>
is the <code>name</code>, the second item of the <code>Array</code> is the <code>value</code>.</p>
<p>Alias for <a href="#urlsearchparamsentries"><code>urlSearchParams.entries()</code></a>.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span class="hljs-title class_">URLSearchParams</span>(<span class="hljs-string">'foo=bar&xyz=baz'</span>);
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> [name, value] <span class="hljs-keyword">of</span> params) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name, value);
}
<span class="hljs-comment">// Prints:</span>
<span class="hljs-comment">// foo bar</span>
<span class="hljs-comment">// xyz baz</span></code> <button class="copy-button">copy</button></pre>
<h4><code>url.domainToASCII(domain)</code><span><a class="mark" href="#urldomaintoasciidomain" id="urldomaintoasciidomain">#</a></span><a aria-hidden="true" class="legacy" id="url_url_domaintoascii_domain"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v20.0.0, v18.17.0</td>
<td><p>ICU requirement is removed.</p></td></tr>
<tr><td>v7.4.0, v6.13.0</td>
<td><p><span>Added in: v7.4.0, v6.13.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>domain</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Returns the <a href="https://tools.ietf.org/html/rfc5891#section-4.4">Punycode</a> ASCII serialization of the <code>domain</code>. If <code>domain</code> is an
invalid domain, the empty string is returned.</p>
<p>It performs the inverse operation to <a href="#urldomaintounicodedomain"><code>url.domainToUnicode()</code></a>.</p>
<pre class="with-32-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> url <span class="hljs-keyword">from</span> <span class="hljs-string">'node:url'</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">domainToASCII</span>(<span class="hljs-string">'español.com'</span>));
<span class="hljs-comment">// Prints xn--espaol-zwa.com</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">domainToASCII</span>(<span class="hljs-string">'中文.com'</span>));
<span class="hljs-comment">// Prints xn--fiq228c.com</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">domainToASCII</span>(<span class="hljs-string">'xn--iñvalid.com'</span>));
<span class="hljs-comment">// Prints an empty string</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">domainToASCII</span>(<span class="hljs-string">'español.com'</span>));
<span class="hljs-comment">// Prints xn--espaol-zwa.com</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">domainToASCII</span>(<span class="hljs-string">'中文.com'</span>));
<span class="hljs-comment">// Prints xn--fiq228c.com</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">domainToASCII</span>(<span class="hljs-string">'xn--iñvalid.com'</span>));
<span class="hljs-comment">// Prints an empty string</span></code><button class="copy-button">copy</button></pre>
<h4><code>url.domainToUnicode(domain)</code><span><a class="mark" href="#urldomaintounicodedomain" id="urldomaintounicodedomain">#</a></span><a aria-hidden="true" class="legacy" id="url_url_domaintounicode_domain"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v20.0.0, v18.17.0</td>
<td><p>ICU requirement is removed.</p></td></tr>
<tr><td>v7.4.0, v6.13.0</td>
<td><p><span>Added in: v7.4.0, v6.13.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>domain</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Returns the Unicode serialization of the <code>domain</code>. If <code>domain</code> is an invalid
domain, the empty string is returned.</p>
<p>It performs the inverse operation to <a href="#urldomaintoasciidomain"><code>url.domainToASCII()</code></a>.</p>
<pre class="with-32-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> url <span class="hljs-keyword">from</span> <span class="hljs-string">'node:url'</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">domainToUnicode</span>(<span class="hljs-string">'xn--espaol-zwa.com'</span>));
<span class="hljs-comment">// Prints español.com</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">domainToUnicode</span>(<span class="hljs-string">'xn--fiq228c.com'</span>));
<span class="hljs-comment">// Prints 中文.com</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">domainToUnicode</span>(<span class="hljs-string">'xn--iñvalid.com'</span>));
<span class="hljs-comment">// Prints an empty string</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">domainToUnicode</span>(<span class="hljs-string">'xn--espaol-zwa.com'</span>));
<span class="hljs-comment">// Prints español.com</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">domainToUnicode</span>(<span class="hljs-string">'xn--fiq228c.com'</span>));
<span class="hljs-comment">// Prints 中文.com</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">domainToUnicode</span>(<span class="hljs-string">'xn--iñvalid.com'</span>));
<span class="hljs-comment">// Prints an empty string</span></code><button class="copy-button">copy</button></pre>
<h4><code>url.fileURLToPath(url[, options])</code><span><a class="mark" href="#urlfileurltopathurl-options" id="urlfileurltopathurl-options">#</a></span><a aria-hidden="true" class="legacy" id="url_url_fileurltopath_url_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v22.1.0</td>
<td><p>The <code>options</code> argument can now be used to determine how to parse the <code>path</code> argument.</p></td></tr>
<tr><td>v10.12.0</td>
<td><p><span>Added in: v10.12.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>url</code> <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The file URL string or URL object to convert to a path.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>windows</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> <code>true</code> if the <code>path</code> should be
return as a windows filepath, <code>false</code> for posix, and
<code>undefined</code> for the system default.
<strong>Default:</strong> <code>undefined</code>.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The fully-resolved platform-specific Node.js file path.</li>
</ul>
<p>This function ensures the correct decodings of percent-encoded characters as
well as ensuring a cross-platform valid absolute path string.</p>
<pre class="with-66-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { fileURLToPath } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:url'</span>;
<span class="hljs-keyword">const</span> __filename = <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-keyword">import</span>.<span class="hljs-property">meta</span>.<span class="hljs-property">url</span>);
<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///C:/path/'</span>).<span class="hljs-property">pathname</span>; <span class="hljs-comment">// Incorrect: /C:/path/</span>
<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///C:/path/'</span>); <span class="hljs-comment">// Correct: C:\path\ (Windows)</span>
<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file://nas/foo.txt'</span>).<span class="hljs-property">pathname</span>; <span class="hljs-comment">// Incorrect: /foo.txt</span>
<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file://nas/foo.txt'</span>); <span class="hljs-comment">// Correct: \\nas\foo.txt (Windows)</span>
<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///你好.txt'</span>).<span class="hljs-property">pathname</span>; <span class="hljs-comment">// Incorrect: /%E4%BD%A0%E5%A5%BD.txt</span>
<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///你好.txt'</span>); <span class="hljs-comment">// Correct: /你好.txt (POSIX)</span>
<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///hello world'</span>).<span class="hljs-property">pathname</span>; <span class="hljs-comment">// Incorrect: /hello%20world</span>
<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///hello world'</span>); <span class="hljs-comment">// Correct: /hello world (POSIX)</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { fileURLToPath } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>);
<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///C:/path/'</span>).<span class="hljs-property">pathname</span>; <span class="hljs-comment">// Incorrect: /C:/path/</span>
<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///C:/path/'</span>); <span class="hljs-comment">// Correct: C:\path\ (Windows)</span>
<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file://nas/foo.txt'</span>).<span class="hljs-property">pathname</span>; <span class="hljs-comment">// Incorrect: /foo.txt</span>
<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file://nas/foo.txt'</span>); <span class="hljs-comment">// Correct: \\nas\foo.txt (Windows)</span>
<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///你好.txt'</span>).<span class="hljs-property">pathname</span>; <span class="hljs-comment">// Incorrect: /%E4%BD%A0%E5%A5%BD.txt</span>
<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///你好.txt'</span>); <span class="hljs-comment">// Correct: /你好.txt (POSIX)</span>
<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///hello world'</span>).<span class="hljs-property">pathname</span>; <span class="hljs-comment">// Incorrect: /hello%20world</span>
<span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///hello world'</span>); <span class="hljs-comment">// Correct: /hello world (POSIX)</span></code><button class="copy-button">copy</button></pre>
<h4><code>url.format(URL[, options])</code><span><a class="mark" href="#urlformaturl-options" id="urlformaturl-options">#</a></span><a aria-hidden="true" class="legacy" id="url_url_format_url_options"></a></h4>
<div class="api_metadata">
<span>Added in: v7.6.0</span>
</div>
<ul>
<li><code>URL</code> <a href="url.html#the-whatwg-url-api" class="type"><URL></a> A <a href="#the-whatwg-url-api">WHATWG URL</a> object</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>auth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the serialized URL string should include the
username and password, <code>false</code> otherwise. <strong>Default:</strong> <code>true</code>.</li>
<li><code>fragment</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the serialized URL string should include the
fragment, <code>false</code> otherwise. <strong>Default:</strong> <code>true</code>.</li>
<li><code>search</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the serialized URL string should include the
search query, <code>false</code> otherwise. <strong>Default:</strong> <code>true</code>.</li>
<li><code>unicode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if Unicode characters appearing in the host
component of the URL string should be encoded directly as opposed to being
Punycode encoded. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Returns a customizable serialization of a URL <code>String</code> representation of a
<a href="#the-whatwg-url-api">WHATWG URL</a> object.</p>
<p>The URL object has both a <code>toString()</code> method and <code>href</code> property that return
string serializations of the URL. These are not, however, customizable in
any way. The <code>url.format(URL[, options])</code> method allows for basic customization
of the output.</p>
<pre class="with-48-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> url <span class="hljs-keyword">from</span> <span class="hljs-string">'node:url'</span>;
<span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://a:b@測試?abc#foo'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
<span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-title function_">toString</span>());
<span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">format</span>(myURL, { <span class="hljs-attr">fragment</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">unicode</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">auth</span>: <span class="hljs-literal">false</span> }));
<span class="hljs-comment">// Prints 'https://測試/?abc'</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>);
<span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://a:b@測試?abc#foo'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
<span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-title function_">toString</span>());
<span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class="hljs-title function_">format</span>(myURL, { <span class="hljs-attr">fragment</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">unicode</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">auth</span>: <span class="hljs-literal">false</span> }));
<span class="hljs-comment">// Prints 'https://測試/?abc'</span></code><button class="copy-button">copy</button></pre>
<h4><code>url.pathToFileURL(path[, options])</code><span><a class="mark" href="#urlpathtofileurlpath-options" id="urlpathtofileurlpath-options">#</a></span><a aria-hidden="true" class="legacy" id="url_url_pathtofileurl_path_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v22.1.0</td>
<td><p>The <code>options</code> argument can now be used to determine how to return the <code>path</code> value.</p></td></tr>
<tr><td>v10.12.0</td>
<td><p><span>Added in: v10.12.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The path to convert to a File URL.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>windows</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> <code>true</code> if the <code>path</code> should be
treated as a windows filepath, <code>false</code> for posix, and
<code>undefined</code> for the system default.
<strong>Default:</strong> <code>undefined</code>.</li>
</ul>
</li>
<li>Returns: <a href="url.html#the-whatwg-url-api" class="type"><URL></a> The file URL object.</li>
</ul>
<p>This function ensures that <code>path</code> is resolved absolutely, and that the URL
control characters are correctly encoded when converting into a File URL.</p>
<pre class="with-66-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { pathToFileURL } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:url'</span>;
<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/foo#1'</span>, <span class="hljs-string">'file:'</span>); <span class="hljs-comment">// Incorrect: file:///foo#1</span>
<span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/foo#1'</span>); <span class="hljs-comment">// Correct: file:///foo%231 (POSIX)</span>
<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/some/path%.c'</span>, <span class="hljs-string">'file:'</span>); <span class="hljs-comment">// Incorrect: file:///some/path%.c</span>
<span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/some/path%.c'</span>); <span class="hljs-comment">// Correct: file:///some/path%25.c (POSIX)</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { pathToFileURL } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>);
<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(__filename); <span class="hljs-comment">// Incorrect: throws (POSIX)</span>
<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(__filename); <span class="hljs-comment">// Incorrect: C:\... (Windows)</span>
<span class="hljs-title function_">pathToFileURL</span>(__filename); <span class="hljs-comment">// Correct: file:///... (POSIX)</span>
<span class="hljs-title function_">pathToFileURL</span>(__filename); <span class="hljs-comment">// Correct: file:///C:/... (Windows)</span>
<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/foo#1'</span>, <span class="hljs-string">'file:'</span>); <span class="hljs-comment">// Incorrect: file:///foo#1</span>
<span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/foo#1'</span>); <span class="hljs-comment">// Correct: file:///foo%231 (POSIX)</span>
<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/some/path%.c'</span>, <span class="hljs-string">'file:'</span>); <span class="hljs-comment">// Incorrect: file:///some/path%.c</span>
<span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/some/path%.c'</span>); <span class="hljs-comment">// Correct: file:///some/path%25.c (POSIX)</span></code><button class="copy-button">copy</button></pre>
<h4><code>url.urlToHttpOptions(url)</code><span><a class="mark" href="#urlurltohttpoptionsurl" id="urlurltohttpoptionsurl">#</a></span><a aria-hidden="true" class="legacy" id="url_url_urltohttpoptions_url"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v19.9.0, v18.17.0</td>
<td><p>The returned object will also contain all the own enumerable properties of the <code>url</code> argument.</p></td></tr>
<tr><td>v15.7.0, v14.18.0</td>
<td><p><span>Added in: v15.7.0, v14.18.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>url</code> <a href="url.html#the-whatwg-url-api" class="type"><URL></a> The <a href="#the-whatwg-url-api">WHATWG URL</a> object to convert to an options object.</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Options object
<ul>
<li><code>protocol</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Protocol to use.</li>
<li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A domain name or IP address of the server to issue the
request to.</li>
<li><code>hash</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The fragment portion of the URL.</li>
<li><code>search</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The serialized query portion of the URL.</li>
<li><code>pathname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The path portion of the URL.</li>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Request path. Should include query string if any.
E.G. <code>'/index.html?page=12'</code>. An exception is thrown when the request path
contains illegal characters. Currently, only spaces are rejected but that
may change in the future.</li>
<li><code>href</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The serialized URL.</li>
<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Port of remote server.</li>
<li><code>auth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Basic authentication i.e. <code>'user:password'</code> to compute an
Authorization header.</li>
</ul>
</li>
</ul>
<p>This utility function converts a URL object into an ordinary options object as
expected by the <a href="http.html#httprequestoptions-callback"><code>http.request()</code></a> and <a href="https.html#httpsrequestoptions-callback"><code>https.request()</code></a> APIs.</p>
<pre class="with-49-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { urlToHttpOptions } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:url'</span>;
<span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://a:b@測試?abc#foo'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">urlToHttpOptions</span>(myURL));
<span class="hljs-comment">/*
{
protocol: 'https:',
hostname: 'xn--g6w251d',
hash: '#foo',
search: '?abc',
pathname: '/',
path: '/?abc',
href: 'https://a:b@xn--g6w251d/?abc#foo',
auth: 'a:b'
}
*/</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { urlToHttpOptions } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>);
<span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://a:b@測試?abc#foo'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">urlToHttpOptions</span>(myURL));
<span class="hljs-comment">/*
{
protocol: 'https:',
hostname: 'xn--g6w251d',
hash: '#foo',
search: '?abc',
pathname: '/',
path: '/?abc',
href: 'https://a:b@xn--g6w251d/?abc#foo',
auth: 'a:b'
}
*/</span></code><button class="copy-button">copy</button></pre>
</section><section><h3>Legacy URL API<span><a class="mark" href="#legacy-url-api" id="legacy-url-api">#</a></span><a aria-hidden="true" class="legacy" id="url_legacy_url_api"></a></h3>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v15.13.0, v14.17.0</td>
<td><p>Deprecation revoked. Status changed to "Legacy".</p></td></tr>
<tr><td>v11.0.0</td>
<td><p>This API is deprecated.</p></td></tr>
</tbody></table>
</details>
</div>
<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use the WHATWG URL API instead.</div><p></p>
<h4>Legacy <code>urlObject</code><span><a class="mark" href="#legacy-urlobject" id="legacy-urlobject">#</a></span><a aria-hidden="true" class="legacy" id="url_legacy_urlobject"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v15.13.0, v14.17.0</td>
<td><p>Deprecation revoked. Status changed to "Legacy".</p></td></tr>
<tr><td>v11.0.0</td>
<td><p>The Legacy URL API is deprecated. Use the WHATWG URL API.</p></td></tr>
</tbody></table>
</details>
</div>
<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use the WHATWG URL API instead.</div><p></p>
<p>The legacy <code>urlObject</code> (<code>require('node:url').Url</code> or
<code>import { Url } from 'node:url'</code>) is
created and returned by the <code>url.parse()</code> function.</p>
<h5><code>urlObject.auth</code><span><a class="mark" href="#urlobjectauth" id="urlobjectauth">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_auth"></a></h5>
<p>The <code>auth</code> property is the username and password portion of the URL, also
referred to as <em>userinfo</em>. This string subset follows the <code>protocol</code> and
double slashes (if present) and precedes the <code>host</code> component, delimited by <code>@</code>.
The string is either the username, or it is the username and password separated
by <code>:</code>.</p>
<p>For example: <code>'user:pass'</code>.</p>
<h5><code>urlObject.hash</code><span><a class="mark" href="#urlobjecthash" id="urlobjecthash">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_hash"></a></h5>
<p>The <code>hash</code> property is the fragment identifier portion of the URL including the
leading <code>#</code> character.</p>
<p>For example: <code>'#hash'</code>.</p>
<h5><code>urlObject.host</code><span><a class="mark" href="#urlobjecthost" id="urlobjecthost">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_host"></a></h5>
<p>The <code>host</code> property is the full lower-cased host portion of the URL, including
the <code>port</code> if specified.</p>
<p>For example: <code>'sub.example.com:8080'</code>.</p>
<h5><code>urlObject.hostname</code><span><a class="mark" href="#urlobjecthostname" id="urlobjecthostname">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_hostname"></a></h5>
<p>The <code>hostname</code> property is the lower-cased host name portion of the <code>host</code>
component <em>without</em> the <code>port</code> included.</p>
<p>For example: <code>'sub.example.com'</code>.</p>
<h5><code>urlObject.href</code><span><a class="mark" href="#urlobjecthref" id="urlobjecthref">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_href"></a></h5>
<p>The <code>href</code> property is the full URL string that was parsed with both the
<code>protocol</code> and <code>host</code> components converted to lower-case.</p>
<p>For example: <code>'http://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash'</code>.</p>
<h5><code>urlObject.path</code><span><a class="mark" href="#urlobjectpath" id="urlobjectpath">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_path"></a></h5>
<p>The <code>path</code> property is a concatenation of the <code>pathname</code> and <code>search</code>
components.</p>
<p>For example: <code>'/p/a/t/h?query=string'</code>.</p>
<p>No decoding of the <code>path</code> is performed.</p>
<h5><code>urlObject.pathname</code><span><a class="mark" href="#urlobjectpathname" id="urlobjectpathname">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_pathname"></a></h5>
<p>The <code>pathname</code> property consists of the entire path section of the URL. This
is everything following the <code>host</code> (including the <code>port</code>) and before the start
of the <code>query</code> or <code>hash</code> components, delimited by either the ASCII question
mark (<code>?</code>) or hash (<code>#</code>) characters.</p>
<p>For example: <code>'/p/a/t/h'</code>.</p>
<p>No decoding of the path string is performed.</p>
<h5><code>urlObject.port</code><span><a class="mark" href="#urlobjectport" id="urlobjectport">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_port"></a></h5>
<p>The <code>port</code> property is the numeric port portion of the <code>host</code> component.</p>
<p>For example: <code>'8080'</code>.</p>
<h5><code>urlObject.protocol</code><span><a class="mark" href="#urlobjectprotocol" id="urlobjectprotocol">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_protocol"></a></h5>
<p>The <code>protocol</code> property identifies the URL's lower-cased protocol scheme.</p>
<p>For example: <code>'http:'</code>.</p>
<h5><code>urlObject.query</code><span><a class="mark" href="#urlobjectquery" id="urlobjectquery">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_query"></a></h5>
<p>The <code>query</code> property is either the query string without the leading ASCII
question mark (<code>?</code>), or an object returned by the <a href="querystring.html"><code>querystring</code></a> module's
<code>parse()</code> method. Whether the <code>query</code> property is a string or object is
determined by the <code>parseQueryString</code> argument passed to <code>url.parse()</code>.</p>
<p>For example: <code>'query=string'</code> or <code>{'query': 'string'}</code>.</p>
<p>If returned as a string, no decoding of the query string is performed. If
returned as an object, both keys and values are decoded.</p>
<h5><code>urlObject.search</code><span><a class="mark" href="#urlobjectsearch" id="urlobjectsearch">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_search"></a></h5>
<p>The <code>search</code> property consists of the entire "query string" portion of the
URL, including the leading ASCII question mark (<code>?</code>) character.</p>
<p>For example: <code>'?query=string'</code>.</p>
<p>No decoding of the query string is performed.</p>
<h5><code>urlObject.slashes</code><span><a class="mark" href="#urlobjectslashes" id="urlobjectslashes">#</a></span><a aria-hidden="true" class="legacy" id="url_urlobject_slashes"></a></h5>
<p>The <code>slashes</code> property is a <code>boolean</code> with a value of <code>true</code> if two ASCII
forward-slash characters (<code>/</code>) are required following the colon in the
<code>protocol</code>.</p>
<h4><code>url.format(urlObject)</code><span><a class="mark" href="#urlformaturlobject" id="urlformaturlobject">#</a></span><a aria-hidden="true" class="legacy" id="url_url_format_urlobject"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v17.0.0</td>
<td><p>Now throws an <code>ERR_INVALID_URL</code> exception when Punycode conversion of a hostname introduces changes that could cause the URL to be re-parsed differently.</p></td></tr>
<tr><td>v15.13.0, v14.17.0</td>
<td><p>Deprecation revoked. Status changed to "Legacy".</p></td></tr>
<tr><td>v11.0.0</td>
<td><p>The Legacy URL API is deprecated. Use the WHATWG URL API.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>URLs with a <code>file:</code> scheme will now always use the correct number of slashes regardless of <code>slashes</code> option. A falsy <code>slashes</code> option with no protocol is now also respected at all times.</p></td></tr>
<tr><td>v0.1.25</td>
<td><p><span>Added in: v0.1.25</span></p></td></tr>
</tbody></table>
</details>
</div>
<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use the WHATWG URL API instead.</div><p></p>
<ul>
<li><code>urlObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A URL object (as returned by <code>url.parse()</code> or
constructed otherwise). If a string, it is converted to an object by passing
it to <code>url.parse()</code>.</li>
</ul>
<p>The <code>url.format()</code> method returns a formatted URL string derived from
<code>urlObject</code>.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>);
url.<span class="hljs-title function_">format</span>({
<span class="hljs-attr">protocol</span>: <span class="hljs-string">'https'</span>,
<span class="hljs-attr">hostname</span>: <span class="hljs-string">'example.com'</span>,
<span class="hljs-attr">pathname</span>: <span class="hljs-string">'/some/path'</span>,
<span class="hljs-attr">query</span>: {
<span class="hljs-attr">page</span>: <span class="hljs-number">1</span>,
<span class="hljs-attr">format</span>: <span class="hljs-string">'json'</span>,
},
});
<span class="hljs-comment">// => 'https://example.com/some/path?page=1&format=json'</span></code> <button class="copy-button">copy</button></pre>
<p>If <code>urlObject</code> is not an object or a string, <code>url.format()</code> will throw a
<a href="errors.html#class-typeerror"><code>TypeError</code></a>.</p>
<p>The formatting process operates as follows:</p>
<ul>
<li>A new empty string <code>result</code> is created.</li>
<li>If <code>urlObject.protocol</code> is a string, it is appended as-is to <code>result</code>.</li>
<li>Otherwise, if <code>urlObject.protocol</code> is not <code>undefined</code> and is not a string, an
<a href="errors.html#class-error"><code>Error</code></a> is thrown.</li>
<li>For all string values of <code>urlObject.protocol</code> that <em>do not end</em> with an ASCII
colon (<code>:</code>) character, the literal string <code>:</code> will be appended to <code>result</code>.</li>
<li>If either of the following conditions is true, then the literal string <code>//</code>
will be appended to <code>result</code>:
<ul>
<li><code>urlObject.slashes</code> property is true;</li>
<li><code>urlObject.protocol</code> begins with <code>http</code>, <code>https</code>, <code>ftp</code>, <code>gopher</code>, or
<code>file</code>;</li>
</ul>
</li>
<li>If the value of the <code>urlObject.auth</code> property is truthy, and either
<code>urlObject.host</code> or <code>urlObject.hostname</code> are not <code>undefined</code>, the value of
<code>urlObject.auth</code> will be coerced into a string and appended to <code>result</code>
followed by the literal string <code>@</code>.</li>
<li>If the <code>urlObject.host</code> property is <code>undefined</code> then:
<ul>
<li>If the <code>urlObject.hostname</code> is a string, it is appended to <code>result</code>.</li>
<li>Otherwise, if <code>urlObject.hostname</code> is not <code>undefined</code> and is not a string,
an <a href="errors.html#class-error"><code>Error</code></a> is thrown.</li>
<li>If the <code>urlObject.port</code> property value is truthy, and <code>urlObject.hostname</code>
is not <code>undefined</code>:
<ul>
<li>The literal string <code>:</code> is appended to <code>result</code>, and</li>
<li>The value of <code>urlObject.port</code> is coerced to a string and appended to
<code>result</code>.</li>
</ul>
</li>
</ul>
</li>
<li>Otherwise, if the <code>urlObject.host</code> property value is truthy, the value of
<code>urlObject.host</code> is coerced to a string and appended to <code>result</code>.</li>
<li>If the <code>urlObject.pathname</code> property is a string that is not an empty string:
<ul>
<li>If the <code>urlObject.pathname</code> <em>does not start</em> with an ASCII forward slash
(<code>/</code>), then the literal string <code>'/'</code> is appended to <code>result</code>.</li>
<li>The value of <code>urlObject.pathname</code> is appended to <code>result</code>.</li>
</ul>
</li>
<li>Otherwise, if <code>urlObject.pathname</code> is not <code>undefined</code> and is not a string, an
<a href="errors.html#class-error"><code>Error</code></a> is thrown.</li>
<li>If the <code>urlObject.search</code> property is <code>undefined</code> and if the <code>urlObject.query</code>
property is an <code>Object</code>, the literal string <code>?</code> is appended to <code>result</code>
followed by the output of calling the <a href="querystring.html"><code>querystring</code></a> module's <code>stringify()</code>
method passing the value of <code>urlObject.query</code>.</li>
<li>Otherwise, if <code>urlObject.search</code> is a string:
<ul>
<li>If the value of <code>urlObject.search</code> <em>does not start</em> with the ASCII question
mark (<code>?</code>) character, the literal string <code>?</code> is appended to <code>result</code>.</li>
<li>The value of <code>urlObject.search</code> is appended to <code>result</code>.</li>
</ul>
</li>
<li>Otherwise, if <code>urlObject.search</code> is not <code>undefined</code> and is not a string, an
<a href="errors.html#class-error"><code>Error</code></a> is thrown.</li>
<li>If the <code>urlObject.hash</code> property is a string:
<ul>
<li>If the value of <code>urlObject.hash</code> <em>does not start</em> with the ASCII hash (<code>#</code>)
character, the literal string <code>#</code> is appended to <code>result</code>.</li>
<li>The value of <code>urlObject.hash</code> is appended to <code>result</code>.</li>
</ul>
</li>
<li>Otherwise, if the <code>urlObject.hash</code> property is not <code>undefined</code> and is not a
string, an <a href="errors.html#class-error"><code>Error</code></a> is thrown.</li>
<li><code>result</code> is returned.</li>
</ul>
<h4><code>url.parse(urlString[, parseQueryString[, slashesDenoteHost]])</code><span><a class="mark" href="#urlparseurlstring-parsequerystring-slashesdenotehost" id="urlparseurlstring-parsequerystring-slashesdenotehost">#</a></span><a aria-hidden="true" class="legacy" id="url_url_parse_urlstring_parsequerystring_slashesdenotehost"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v19.0.0, v18.13.0</td>
<td><p>Documentation-only deprecation.</p></td></tr>
<tr><td>v15.13.0, v14.17.0</td>
<td><p>Deprecation revoked. Status changed to "Legacy".</p></td></tr>
<tr><td>v11.14.0</td>
<td><p>The <code>pathname</code> property on the returned URL object is now <code>/</code> when there is no path and the protocol scheme is <code>ws:</code> or <code>wss:</code>.</p></td></tr>
<tr><td>v11.0.0</td>
<td><p>The Legacy URL API is deprecated. Use the WHATWG URL API.</p></td></tr>
<tr><td>v9.0.0</td>
<td><p>The <code>search</code> property on the returned URL object is now <code>null</code> when no query string is present.</p></td></tr>
<tr><td>v0.1.25</td>
<td><p><span>Added in: v0.1.25</span></p></td></tr>
</tbody></table>
</details>
</div>
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use the WHATWG URL API instead.</div><p></p>
<ul>
<li><code>urlString</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The URL string to parse.</li>
<li><code>parseQueryString</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, the <code>query</code> property will always
be set to an object returned by the <a href="querystring.html"><code>querystring</code></a> module's <code>parse()</code>
method. If <code>false</code>, the <code>query</code> property on the returned URL object will be an
unparsed, undecoded string. <strong>Default:</strong> <code>false</code>.</li>
<li><code>slashesDenoteHost</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, the first token after the literal
string <code>//</code> and preceding the next <code>/</code> will be interpreted as the <code>host</code>.
For instance, given <code>//foo/bar</code>, the result would be
<code>{host: 'foo', pathname: '/bar'}</code> rather than <code>{pathname: '//foo/bar'}</code>.
<strong>Default:</strong> <code>false</code>.</li>
</ul>
<p>The <code>url.parse()</code> method takes a URL string, parses it, and returns a URL
object.</p>
<p>A <code>TypeError</code> is thrown if <code>urlString</code> is not a string.</p>
<p>A <code>URIError</code> is thrown if the <code>auth</code> property is present but cannot be decoded.</p>
<p><code>url.parse()</code> uses a lenient, non-standard algorithm for parsing URL
strings. It is prone to security issues such as <a href="https://hackerone.com/reports/678487">host name spoofing</a>
and incorrect handling of usernames and passwords. Do not use with untrusted
input. CVEs are not issued for <code>url.parse()</code> vulnerabilities. Use the
<a href="#the-whatwg-url-api">WHATWG URL</a> API instead.</p>
<h4><code>url.resolve(from, to)</code><span><a class="mark" href="#urlresolvefrom-to" id="urlresolvefrom-to">#</a></span><a aria-hidden="true" class="legacy" id="url_url_resolve_from_to"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v15.13.0, v14.17.0</td>
<td><p>Deprecation revoked. Status changed to "Legacy".</p></td></tr>
<tr><td>v11.0.0</td>
<td><p>The Legacy URL API is deprecated. Use the WHATWG URL API.</p></td></tr>
<tr><td>v6.6.0</td>
<td><p>The <code>auth</code> fields are now kept intact when <code>from</code> and <code>to</code> refer to the same host.</p></td></tr>
<tr><td>v6.0.0</td>
<td><p>The <code>auth</code> fields is cleared now the <code>to</code> parameter contains a hostname.</p></td></tr>
<tr><td>v6.5.0, v4.6.2</td>
<td><p>The <code>port</code> field is copied correctly now.</p></td></tr>
<tr><td>v0.1.25</td>
<td><p><span>Added in: v0.1.25</span></p></td></tr>
</tbody></table>
</details>
</div>
<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use the WHATWG URL API instead.</div><p></p>
<ul>
<li><code>from</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The base URL to use if <code>to</code> is a relative URL.</li>
<li><code>to</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The target URL to resolve.</li>
</ul>
<p>The <code>url.resolve()</code> method resolves a target URL relative to a base URL in a
manner similar to that of a web browser resolving an anchor tag.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:url'</span>);
url.<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'/one/two/three'</span>, <span class="hljs-string">'four'</span>); <span class="hljs-comment">// '/one/two/four'</span>
url.<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'http://example.com/'</span>, <span class="hljs-string">'/one'</span>); <span class="hljs-comment">// 'http://example.com/one'</span>
url.<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'http://example.com/one'</span>, <span class="hljs-string">'/two'</span>); <span class="hljs-comment">// 'http://example.com/two'</span></code> <button class="copy-button">copy</button></pre>
<p>To achieve the same result using the WHATWG URL API:</p>
<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">resolve</span>(<span class="hljs-params"><span class="hljs-keyword">from</span>, to</span>) {
<span class="hljs-keyword">const</span> resolvedUrl = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(to, <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-keyword">from</span>, <span class="hljs-string">'resolve://'</span>));
<span class="hljs-keyword">if</span> (resolvedUrl.<span class="hljs-property">protocol</span> === <span class="hljs-string">'resolve:'</span>) {
<span class="hljs-comment">// `from` is a relative URL.</span>
<span class="hljs-keyword">const</span> { pathname, search, hash } = resolvedUrl;
<span class="hljs-keyword">return</span> pathname + search + hash;
}
<span class="hljs-keyword">return</span> resolvedUrl.<span class="hljs-title function_">toString</span>();
}
<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'/one/two/three'</span>, <span class="hljs-string">'four'</span>); <span class="hljs-comment">// '/one/two/four'</span>
<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'http://example.com/'</span>, <span class="hljs-string">'/one'</span>); <span class="hljs-comment">// 'http://example.com/one'</span>
<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'http://example.com/one'</span>, <span class="hljs-string">'/two'</span>); <span class="hljs-comment">// 'http://example.com/two'</span></code> <button class="copy-button">copy</button></pre>
<p><a id="whatwg-percent-encoding"></a></p>
</section><section><h3>Percent-encoding in URLs<span><a class="mark" href="#percent-encoding-in-urls" id="percent-encoding-in-urls">#</a></span><a aria-hidden="true" class="legacy" id="url_percent_encoding_in_urls"></a></h3>
<p>URLs are permitted to only contain a certain range of characters. Any character
falling outside of that range must be encoded. How such characters are encoded,
and which characters to encode depends entirely on where the character is
located within the structure of the URL.</p>
<h4>Legacy API<span><a class="mark" href="#legacy-api" id="legacy-api">#</a></span><a aria-hidden="true" class="legacy" id="url_legacy_api"></a></h4>
<p>Within the Legacy API, spaces (<code>' '</code>) and the following characters will be
automatically escaped in the properties of URL objects:</p>
<pre><code class="language-text">< > " ` \r \n \t { } | \ ^ '</code> <button class="copy-button">copy</button></pre>
<p>For example, the ASCII space character (<code>' '</code>) is encoded as <code>%20</code>. The ASCII
forward slash (<code>/</code>) character is encoded as <code>%3C</code>.</p>
<h4>WHATWG API<span><a class="mark" href="#whatwg-api" id="whatwg-api">#</a></span><a aria-hidden="true" class="legacy" id="url_whatwg_api"></a></h4>
<p>The <a href="https://url.spec.whatwg.org/">WHATWG URL Standard</a> uses a more selective and fine grained approach to
selecting encoded characters than that used by the Legacy API.</p>
<p>The WHATWG algorithm defines four "percent-encode sets" that describe ranges
of characters that must be percent-encoded:</p>
<ul>
<li>
<p>The <em>C0 control percent-encode set</em> includes code points in range U+0000 to
U+001F (inclusive) and all code points greater than U+007E (~).</p>
</li>
<li>
<p>The <em>fragment percent-encode set</em> includes the <em>C0 control percent-encode set</em>
and code points U+0020 SPACE, U+0022 ("), U+003C (<), U+003E (>),
and U+0060 (`).</p>
</li>
<li>
<p>The <em>path percent-encode set</em> includes the <em>C0 control percent-encode set</em>
and code points U+0020 SPACE, U+0022 ("), U+0023 (#), U+003C (<), U+003E (>),
U+003F (?), U+0060 (`), U+007B ({), and U+007D (}).</p>
</li>
<li>
<p>The <em>userinfo encode set</em> includes the <em>path percent-encode set</em> and code
points U+002F (/), U+003A (:), U+003B (;), U+003D (=), U+0040 (@),
U+005B ([) to U+005E(^), and U+007C (|).</p>
</li>
</ul>
<p>The <em>userinfo percent-encode set</em> is used exclusively for username and
passwords encoded within the URL. The <em>path percent-encode set</em> is used for the
path of most URLs. The <em>fragment percent-encode set</em> is used for URL fragments.
The <em>C0 control percent-encode set</em> is used for host and path under certain
specific conditions, in addition to all other cases.</p>
<p>When non-ASCII characters appear within a host name, the host name is encoded
using the <a href="https://tools.ietf.org/html/rfc5891#section-4.4">Punycode</a> algorithm. Note, however, that a host name <em>may</em> contain
<em>both</em> Punycode encoded and percent-encoded characters:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://%CF%80.example.com/foo'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">href</span>);
<span class="hljs-comment">// Prints https://xn--1xa.example.com/foo</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span class="hljs-property">origin</span>);
<span class="hljs-comment">// Prints https://xn--1xa.example.com</span></code> <button class="copy-button">copy</button></pre></section>
<!-- API END -->
</div>
</div>
</div>
</body>
</html>
|