如何使用 javascript 从 url 获取域名
How to get a domain name from the url using javascript
我有一个像 'https://abc.example.com/pqr/controllername/actionname/id' 这样的域名,所以我只想要 'https://abc.example.com/pqr/' [=29] =] 通过使用 JavaScript 我如何获得它?
我试过了 - 1) window.location.origin 2) window.location.host- 它只会 return https://abc.example.com
使用URL
-API
let x = new URL("https://somefakedomain.abcd.com/pqr/controllername/actionname/id");
// take your pick
console.log(`${x.origin}/${x.pathname.split("/")[1]}/`);
console.log(`${x.origin}/${x.pathname.slice(1).split("/")[0]}/`);
console.log(`${x.toString().substr(0, x.toString().indexOf("pqr") + 4)}`);
console.log(`${x.origin}/${x.pathname.substr(x.pathname.indexOf("pqr"), 4)}`);
我有一个像 'https://abc.example.com/pqr/controllername/actionname/id' 这样的域名,所以我只想要 'https://abc.example.com/pqr/' [=29] =] 通过使用 JavaScript 我如何获得它?
我试过了 - 1) window.location.origin 2) window.location.host- 它只会 return https://abc.example.com
使用URL
-API
let x = new URL("https://somefakedomain.abcd.com/pqr/controllername/actionname/id");
// take your pick
console.log(`${x.origin}/${x.pathname.split("/")[1]}/`);
console.log(`${x.origin}/${x.pathname.slice(1).split("/")[0]}/`);
console.log(`${x.toString().substr(0, x.toString().indexOf("pqr") + 4)}`);
console.log(`${x.origin}/${x.pathname.substr(x.pathname.indexOf("pqr"), 4)}`);