📜  节点 | URL.origin API

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

节点 | URL.origin API

url.origin是 url 模块中URL 类的内置应用程序编程接口 (API)。
url.origin API 用于获取 URL 源的只读序列化。

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

示例 1:

javascript
//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);


javascript
//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);


输出:



注意:我们无法使用 url.origin API 设置 URL 的来源。如果我们尝试这样做,那么它将被忽略并且不会影响起源。
示例 2:

javascript

//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);

输出: