如何根据url hash执行js函数 url#nameoffunction

发布于 2024-11-08 18:15:48 字数 218 浏览 0 评论 0原文

我看到一些网站根据 URL 中的内容执行 JavaScript 函数。例如,

当我访问 http://domain.com/jobs#test

时,网站会执行一个函数基于#test

我可以通过检查location.href来做到这一点,但是有更好的方法吗?

I saw some of websites executes a JavaScript function based on has in the URL. For example,

when I access http://domain.com/jobs#test

then the website executes a function based on #test

I can do it by checking location.href, but is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

末蓝 2024-11-15 18:15:48

这就是我所做的:

window.onload = function(){
    var hash = (window.location.hash).replace('#', '');
    if (hash.length == 0) {
        //no hash do something
    }
    else {
        //else do something with hash
    }
}

演示: http://jsfiddle.net/maniator/XCjpy/show/ #测试
演示2: http://jsfiddle.net/maniator/XCjpy/show/
演示3: http://jsfiddle.net/maniator/XCjpy/show/#testing_again

This is what i do:

window.onload = function(){
    var hash = (window.location.hash).replace('#', '');
    if (hash.length == 0) {
        //no hash do something
    }
    else {
        //else do something with hash
    }
}

demo: http://jsfiddle.net/maniator/XCjpy/show/#test
demo2: http://jsfiddle.net/maniator/XCjpy/show/
demo3: http://jsfiddle.net/maniator/XCjpy/show/#testing_again

贱人配狗天长地久 2024-11-15 18:15:48

如果你不需要支持像 IE6 和 IE7 这样的旧浏览器,你可以使用:

window.onhashchange = function(){
  switch(location.hash) {
    case '#hash1':
      //do something
    break;
    case '#has2':
      //do something else
    break;
  }
}

但如果你必须支持旧浏览器,你需要轮询:

var oldHash = location.hash;
setInterval(function(){
  if(location.hash !== oldHash){
    oldHash = location.hash;
    //hash changed do something
  }
}, 120);

If you don't need to support old browsers like IE6 and IE7 you can use:

window.onhashchange = function(){
  switch(location.hash) {
    case '#hash1':
      //do something
    break;
    case '#has2':
      //do something else
    break;
  }
}

But if you have to support older browsers you need to poll:

var oldHash = location.hash;
setInterval(function(){
  if(location.hash !== oldHash){
    oldHash = location.hash;
    //hash changed do something
  }
}, 120);
堇年纸鸢 2024-11-15 18:15:48

现场演示

$(window).bind('hashchange', function() {
    var hash = document.location.hash;
    var func = hash.replace('#', '');
    eval(func + '()');
});

function asdf() {
    alert('asdf function');
}

function qwerty() {
    alert('qwerty function');
}

注意: eval() 很危险。您应该创建一个预定义的安全函数数组,并调用它们。

Live Demo

$(window).bind('hashchange', function() {
    var hash = document.location.hash;
    var func = hash.replace('#', '');
    eval(func + '()');
});

function asdf() {
    alert('asdf function');
}

function qwerty() {
    alert('qwerty function');
}

Note: eval() is dangerous. You should make a predefined array of safe functions, and call those.

看轻我的陪伴 2024-11-15 18:15:48

看看这个

参见属性表。
location.hash 应该对你有帮助。

Have a look at This.

See the property table.
location.hash should help you.

终弃我 2024-11-15 18:15:48

您可以使用像 YUI 这样的库来更优雅地完成此操作,看看 YUI 浏览器历史记录管理器

http: //developer.yahoo.com/yui/history/

You could use a library like YUI to do this more elegantly, Take a look at the YUI Browser History Manager

http://developer.yahoo.com/yui/history/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文