📜  javascript 新网址 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:40.908000             🧑  作者: Mango

代码示例1
const url = new URL(url [, base])
// KEEP IN MIND that new URL(...) adds the first parameter to the second one

// examples:

// Base urls
let m = 'https://developer.mozilla.org';

new URL('en-US/docs', m); 
// => 'https://developer.mozilla.org/en-US/docs'
// adds the first parameter to second one

new URL('/en-US/docs', "https://developer.mozilla.org/fr-FR/toto");
// => 'https://developer.mozilla.org/en-US/docs'
// the / in start of '/en-US/docs' makes the link start from base and ignore other routes

new URL('en-US/docs', "https://developer.mozilla.org/fr-FR/toto");
// => 'https://developer.mozilla.org/fr-FR/en-US/docs'
// this time the url starts from the last route, then it adds first parameter to it

new URL('http://www.example.com', m);
// => 'http://www.example.com/'
// first param replaces second one because it has it's own origin