如何在url包含锚点时调用js函数

Iul*_*ese 0 javascript jquery

我有一个网站example.com.当我访问此页面example.com/#login时,我想调用js警报功能.我怎样才能做到这一点?

Ror*_*san 5

// example.com/#login
if (window.location.hash) {
    alert("URL has hash");
    var hash = window.location.hash; // = "#login"
    callMyFunction();
}
Run Code Online (Sandbox Code Playgroud)