在JavaScript中,你可以通过window.location.href
来获取当前页面的URL。下面是一个简单的例子:
var currentURL = window.location.href;
console.log(currentURL);
上述代码获取当前页面的完整URL,并将其存储在currentURL
变量中。然后,通过console.log
语句将其打印到控制台,以便查看。
如果你只希望获取URL的某些部分,比如路径或查询参数,可以使用window.location
对象的其他属性,例如:
window.location.protocol
:获取协议部分(例如:"http:"或"https:")。window.location.host
:获取主机部分(例如:"www.example.com")。window.location.pathname
:获取路径部分(例如:"/path/page.html")。window.location.search
:获取查询参数部分(例如:"?key1=value1&key2=