[ Web Proxy ]
URL:
Viewing: https://uk.javascript.info/url [Back]  [Original]

URL
:
DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek

URL

URL URL .

, , , URL . URL. .

URL

URL :

new URL(url, [base])
  • url URL , , ( ),
  • base URL, .

:

let url = new URL('https://javascript.info/profile/admin');

URL:

let url1 = new URL('https://javascript.info/profile/admin');
let url2 = new URL('/profile/admin', 'https://javascript.info');

alert(url1); // https://javascript.info/profile/admin
alert(url2); // https://javascript.info/profile/admin

URL URL, :

let url = new URL('https://javascript.info/profile/admin');
let newUrl = new URL('tester', url);

alert(newUrl); // https://javascript.info/profile/tester

URL , URL :

let url = new URL('https://javascript.info/url');

alert(url.protocol); // https:
alert(url.host);     // javascript.info
alert(url.pathname); // /url

URL :

[]
  • href URL-, , url.toString()
  • protocol , :
  • search , ?
  • hash #
  • user password, HTTP : http://login:password@site.com ( , ).
URL ,

fetch XMLHttpRequest URL , URL.

, URL - , , URL-.

?

, URL- , , https://google.com/search?query=JavaScript.

, , URL-:

new URL('https://google.com/search?query=JavaScript')

, , ( ).

, URL : url.searchParams, URLSearchParams.

:

  • append(name, value) name,
  • delete(name) name,
  • get(name) name,
  • getAll(name) , name (, ?user=John&user=Pete),
  • has(name) name,
  • set(name, value) / name,
  • sort() , ,
  • , Map.

, :

let url = new URL('https://google.com/search');

url.searchParams.set('q', 'test me!'); //      !

alert(url); // https://google.com/search?q=test+me%21

url.searchParams.set('tbs', 'qdr:y'); //     :

//   
alert(url); // https://google.com/search?q=test+me%21&tbs=qdr%3Ay

//       (   )
for(let [name, value] of url.searchParams) {
  alert(`${name}=${value}`); // q=test me!, then tbs=qdr:y
}

, URL-, RFC3986.

, , . , UTF-8 , %. %20 ( +).

, URL . , URL :

//     

let url = new URL('https://uk.wikipedia.org/wiki/');

url.searchParams.set('key', '');
alert(url); // https://uk.wikipedia.org/wiki/%D0%A2%D0%B5%D1%81%D1%82?key=%D1%97

, , .

URL- , UTF-8, %...

URL , URL-.

, URL , . , .

, , .

:

: encodeURIComponent encodeURI? ?

, URL-, .

https://site.com:8080/path/page?p1=v1&p2=v2#hash

, :, ?, =, &, # URL.

, URL, , .

  • encodeURI , URL.
  • encodeURIComponent : #, $, &, +, ,, /, :, ;, =, ? @.

, URL encodeURI:

//      url
let url = encodeURI('http://site.com/');

alert(url); // http://site.com/%D0%BF%D1%80%D0%B8%D0%B2%D1%96%D1%82

, URL encodeURIComponent:

let music = encodeURIComponent('Rock&Roll');

let url = `https://google.com/search?q=${music}`;
alert(url); // https://google.com/search?q=Rock%26Roll

encodeURI:

let music = encodeURI('Rock&Roll');

let url = `https://google.com/search?q=${music}`;
alert(url); // https://google.com/search?q=Rock&Roll

, encodeURI &, URL.

& , q=Rock&Roll, q=Rock Roll. , .

encodeURIComponent URL. , , , .

URL

URL URLSearchParams URL: RFC3986, encode* RFC2396.

, IPv6 -:

// valid url with IPv6 address
let url = 'http://[2607:f8b0:4005:802::1007]/';

alert(encodeURI(url)); // http://%5B2607:f8b0:4005:802::1007%5D/
alert(new URL(url)); // http://[2607:f8b0:4005:802::1007]/

, encodeURI [...], , , IPv6 RFC2396 ( 1998).

, encode* .

,

Web Proxy Viewer  |  New URL  |  Original Page