Get URL in JavaScript: Complete Guide
In JavaScript, the window.location.href property can be used to accurately obtain the URL of the current page. This property returns a string that includes the complete URL, including the protocol (such as http or https), host name, port number, path, and query parameters.
Here is a simple example code showing how to use window.location.href to retrieve the URL information of the current page.
// 获取当前页面的URL地址信息
var currentUrl = window.location.href;
console.log(currentUrl);
The variable currentUrl will contain the complete URL address of the current page by using the above code, and it can be printed to the console for viewing using the console.log() method.
Using the window.location.href property is a common and convenient way to access the URL information of the current page, which is frequently used when developing web applications.