如何在 Chrome 控制台中使用 JavaScript 检查 window.locaton 是否成功加载?

问题描述

如何检查 window.locaton 是否加载成功? 我想打开一个 chrome 代码段以重定向一个新的 URL 并使用 console.log 获取新的 URL 链接

var long_url = "https://stackoverflow.com/";
var long_url1 = "https://google.com/";
window.location.replace(long_url);
//How to get new URL link "https://stackoverflow.com/"?
window.location.replace(long_url1);
//How to get new URL link "https://google.com/" again?

解决方法

要获取当前页面的 URL 尝试

window.location.href

更准确的信息

var pathname = window.location.pathname; // Returns path only (/path/example.html)
var url      = window.location.href;     // Returns full URL (https://example.com/path/example.html)
var origin   = window.location.origin;   // Returns base URL (https://example.com)
,

您可以使用 window.location.href 获取当前 URL。所以你可以写:

var long_url = "https://stackoverflow.com/";
var long_url1 = "https://google.com/";
window.location.replace(long_url);
console.log(window.location.href);
window.location.replace(long_url1);
console.log(window.location.href);

要在重定向到新 URL 后查看控制台日志,您可以转到检查 -> 控制台 -> 设置 -> 检查保留日志。通过这样做,它将帮助您在重定向之前或之后查看所有控制台日志 Check this image for viewing console settings