当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Node.js URL.href用法及代码示例

url.href是类的内置应用程序编程接口URL与在网址获取和设置序列化 URL 的模块。获取 href 属性的值相当于调用网址.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); 

输出:

示例 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



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Node.js URL.href API。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。