Node.js 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