📜  节点 | URL.hash API

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

节点 | URL.hash API

url.hash是 url模块中类 URL的内置应用程序编程接口,用于获取和设置 URL 的片段部分。

句法:

url.hash

返回值:获取和设置 URL 的片段部分。

下面的程序说明了url.hash方法的使用:

示例 1:



Javascript
// node program to demonstrate the
// url.hash API as Setter
 
// creating and initializing myURL
const myURL = new URL('https://example.org/foo#ram');
 
// Display href value of myURL before change
console.log("Before Change");
console.log(myURL.href);
 
// assigning fragment portion
// using hash
console.log();
myURL.hash = 'rahim';
 
// Display href value of myURL after change
console.log("After Change");
console.log(myURL.href);


Javascript
// node program to demonstrate the
// url.hash API as Getter
 
// creating and initializing myURL
const myURL = new URL('https://example.org/foo#ram');
 
// getting the fragment portion
// using hash
const hash = myURL.hash;
 
// Display hash value
console.log(hash);


输出

Before Change
https://example.org/foo#ram

After Change
https://example.org/foo#rahim

示例 2:

Javascript

// node program to demonstrate the
// url.hash API as Getter
 
// creating and initializing myURL
const myURL = new URL('https://example.org/foo#ram');
 
// getting the fragment portion
// using hash
const hash = myURL.hash;
 
// Display hash value
console.log(hash);

输出:

#ram

注意:上述程序可以使用node myapp.js命令运行。

参考:
https://nodejs.org/dist/latest-v10.x/docs/api/url.html#url_url_hash