30秒学会 JavaScript 片段 · 2023年2月16日

30秒学会 JavaScript 片段 – How can I get the current URL in JavaScript?

As mentioned in the Window.location Cheat Sheet, JavaScript provides a number of properties and methods to work with the current URL. Among those, Window.location.href provides the easiest way to get the current URL as a string.

代码实现

const currentURL = () => window.location.href;

currentURL(); // 'https://www.google.com/'

翻译自:https://www.30secondsofcode.org/js/s/current-url