Node.js URL.href API
url.href是类URL的内置应用程序编程接口,在url模块中获取和设置序列化的 URL。获取 href 属性的值相当于调用url.toString()方法。将此属性的值设置为一个新值相当于使用 new URL(value) 创建一个新的 URL 对象。每个 URL 对象的属性都将被修改。
句法:
const url.href
返回值:获取并设置序列化的 URL。
下面的程序说明了使用url.href方法:
示例 1:
javascript
// node program to demonstrate the
// url.href API as Setter
//importing the module 'url'
const http = require('url');
// creating and initializing myURL
const myURL = new URL('https://example.com:80/foo#ram');
// Display href value of myURL before change
console.log("Before Change");
console.log(myURL.href);
// assigning serialized URL
// using href
console.log();
myURL.href = 'https://example.com/bar';
// Display href value of myURL after change
console.log("After Change");
console.log(myURL.href);
javascript
// node program to demonstrate the
// url.href API as Getter
//importing the module 'url'
const http = require('url');
// creating and initializing myURL
const myURL = new URL('https://example.org/foo#ram');
// getting the serialized URL
// using href
const href = myURL.href;
// Display hostname value
console.log(href);
输出:
示例 2:
javascript
// node program to demonstrate the
// url.href API as Getter
//importing the module 'url'
const http = require('url');
// creating and initializing myURL
const myURL = new URL('https://example.org/foo#ram');
// getting the serialized URL
// using href
const href = myURL.href;
// Display hostname value
console.log(href);
输出:
参考:
https://nodejs.org/api/url.html#url_url_href