如何在更改window.location后停止执行

How to stop execution after changing window.location?

本文关键字:执行 location window      更新时间:2023-09-26

假设我在脚本的顶部有一些移动检查:

if (isMobile) {
    window.location.href = '/m/' + window.location.pathname + window.location.search;
}

我在代码中也有一些度量(或者,甚至在异步加载的其他模块中),这些度量正在发送到我的度量服务器。

我的问题是:如何停止执行window.location更改以下的所有内容?或者更好的是,如何停止整个javascript引擎,使其不执行任何其他异步加载的模块?

我知道我可以用很多不同的方式进行移动代理,但在这个任务中,我的移动重定向必须在客户端运行,这是既定的。

if (isMobile) {
    window.location.href = '/m/' + window.location.pathname + window.location.search;
    return;
}

很简单:return false。。。

if (isMobile) {
    window.location.href = '/m/' + window.location.pathname + window.location.search;
    return false;
}

有一个用javascript实现的exit()函数。

如何在Javascript 中终止脚本