當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Node.js URL.origin用法及代碼示例


url.origin是url模塊中URL類的內置應用程序編程接口(API)。 url.origin API用於獲取URL來源的隻讀序列化。

Syntax:url.origin
url:It is an object created by URL constructor.

範例1:

//Importing the url module 
const url = require('url'); 
  
//Creating an URL_1 object with URL constructor. 
const URL_1 = new URL("https://www.geeksforgeeks.org/geeks"); 
  
//Getting origin of above created URL_1 object 
console.log(URL_1.origin);

輸出:


https://www.geeksforgeeks.org

注意:我們無法使用url.origin API設置URL的來源。如果我們嘗試這樣做,那麽它將被忽略並且不會影響到原點。

範例2:

//Importing the url module 
const url = require('url'); 
  
//Creating an URL_1 object with URL constructor. 
const URL_1 = new URL("https://www.geeksforgeeks.org/geeks"); 
  
//Getting origin of above created URL_1 object 
console.log(URL_1.origin); 
  
//Setting URL_1 origin to https://www.geeks.com 
URL_1.origin = "https://www.geeks.com"; 
  
//Getting origin after setting URL_1 origin 
console.log(URL_1.origin);

輸出:

https://www.geeksforgeeks.org


相關用法


注:本文由純淨天空篩選整理自AshishkrGoyal大神的英文原創作品 Node | URL.origin API。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。