检测后退按钮/哈希变化网址 [英] Detecting Back Button/Hash Change in URL

查看:156
本文介绍了检测后退按钮/哈希变化网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚成立了我的新主页: http://ritter.vg 。我使用jQuery,但非常最低限度。
它加载全部采用AJAX网页 - 我把它设置为允许书签通过在URL中检测的哈希值。

I just set up my new homepage at http://ritter.vg. I'm using jQuery, but very minimally.
It loads all the pages using AJAX - I have it set up to allow bookmarking by detecting the hash in the URL.

 //general functions
 function getUrl(u) {
      return u + '.html';
 }
 function loadURL(u)    {
      $.get(getUrl(u), function(r){
                $('#main').html(r);
           }
      );
 }
 //allows bookmarking
 var hash = new String(document.location).indexOf("#");
 if(hash > 0)
 {
      page = new String(document.location).substring(hash + 1);
      if(page.length > 1)
        loadURL(page);
      else
        loadURL('news');
 }
 else
      loadURL('news');

但我不能让后退和前进按钮的工作。

But I can't get the back and forward buttons to work.

有没有一种方法来检测时,后退按钮已经pssed $ P $(或检测时,散的变化),而不使用的setInterval循环?当我试图那些0.2 1秒超时,就挂我的CPU。

Is there a way to detect when the back button has been pressed (or detect when the hash changes) without using a setInterval loop? When I tried those with .2 and 1 second timeouts, it pegged my CPU.

推荐答案

使用 jQuery的hashchange事件的插件来代替。关于你提到的全AJAX导航,尝试有搜索引擎友好的AJAX 。否则,什么都不显示在浏览器页面的JavaScript限制。

Use the jQuery hashchange event plugin instead. Regarding your full ajax navigation, try to have SEO friendly ajax. Otherwise your pages shown nothing in browsers with JavaScript limitations.

这篇关于检测后退按钮/哈希变化网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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