Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当url包含锚点时如何调用js函数_Javascript_Jquery - Fatal编程技术网

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

Javascript 当url包含锚点时如何调用js函数,javascript,jquery,Javascript,Jquery,我有一个网站example.com。 当我访问example.com/#login这个页面时,我想调用一个js警报函数。 我怎样才能做到这一点?您可以查看“登录”的URL。如果存在,则调用函数。 我推荐jQuery,这种方法: 您可以检查URL以获取登录信息。如果存在,则调用函数。 我推荐jQuery,这种方法: 如果您想要一个好的跨浏览器库来处理url,我会查看History.js 还有Backbone.js,这可能比你需要的多得多,因为它是一个MV*框架,但是它有一个路由器的概念,这也

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

您可以查看“登录”的URL。如果存在,则调用函数。

我推荐jQuery,这种方法:

您可以检查URL以获取登录信息。如果存在,则调用函数。

我推荐jQuery,这种方法:

如果您想要一个好的跨浏览器库来处理url,我会查看History.js

还有Backbone.js,这可能比你需要的多得多,因为它是一个MV*框架,但是它有一个路由器的概念,这也很有用


如果您想要一个好的跨浏览器库来处理url,我会查看History.js

还有Backbone.js,这可能比你需要的多得多,因为它是一个MV*框架,但是它有一个路由器的概念,这也很有用


我认为您可以检查
window.location.hash
是否不是一个错误的值,以检查URL中是否存在哈希

例如:

function someAlert(message) {
    // This would be your alert function. This is just for example
    alert(message);
}
// The "if the URL has a hash":
if (window.location.hash) {
    someAlert("Hello URL with a hash visitor!"); // "Hello URL with a hash visitor!" can be whatever you want
}
如果要保存后面的内容并包括哈希,只需使用:

var hash = window.location.hash; // hash would be "#login" if the URL was http://example.com/#login

我想你可以检查
window.location.hash
是否不是一个错误的值来检查URL中是否有哈希

例如:

function someAlert(message) {
    // This would be your alert function. This is just for example
    alert(message);
}
// The "if the URL has a hash":
if (window.location.hash) {
    someAlert("Hello URL with a hash visitor!"); // "Hello URL with a hash visitor!" can be whatever you want
}
如果要保存后面的内容并包括哈希,只需使用:

var hash = window.location.hash; // hash would be "#login" if the URL was http://example.com/#login