URL更改事件 - JavaScript [英] URL Change Event - JavaScript

查看:101
本文介绍了URL更改事件 - JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个setTimeout,它在一个名为newURL的变量中记录URL。

I have a setTimeout that records the URL in a variable called newURL.

function checkURL() {
    var newURL = window.location;
} setInterval(checkURL, 1000);

我想要发生的是,当URL改变时,必须执行某个功能。请问有人可以帮助我吗?

What I would like to happen, is that a certain function must be executed, when the URL changes. Could someone help me here please?

推荐答案

不确定为什么页面会在您捕获网址中的更改之前不会重新加载。
您还可以查看 window.onunload

Not sure why the page wouldn't reload before you could catch the change in the url. You can also take a look a window.onunload

var checkURL = (function () {
    var oldURL = location.href;
    return function (fn) {
        var newURL = location.href;
        if (fn && oldURL !== newURL) {
            fn(oldURL, newURL);
        }
        oldURL = newURL;
    };
}());

这篇关于URL更改事件 - JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆