📜  节点 | URL.href API

📅  最后修改于: 2022-05-13 01:56:57.012000             🧑  作者: Mango

节点 | 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