2012-02-07 147 views
0

可能重复:
Making Browser Back button work while using AJAX requests浏览器后退按钮/ #one #two等

我想写一些jQuery的是,当浏览器的URL变化(不刷新),它会做的行动。

我会100%知道的网址是什么,从而他们会

  • www.hello.com/#one
  • www.hello.com/#two
  • www.hello。 com /#三

我不太在意行动(我可以排序);更重要的是在未刷新时检测到URL已更改。

任何指针?

我不想使用插件,因为我觉得它们对我所需要的东西是过度的,因为我知道我想要实现的具体细节。

+0

谢谢你生病给我看一下 – odd 2012-02-07 17:27:16

回答

1

退房从我的一个项目采取了以下代码(http://yankele.co.il

$(document).ready(function() { 

    //Declare some variables... 
    var $mainContent = $('.main-content'), 
     contentHeight = $mainContent.height(), 
     siteURL = 'http://' + top.location.host.toString(), 
     hash = window.location.hash, 
     $internalLinks = $('a[href^=' + siteURL + ']'), 
     $ajaxSpinner = $('#spinner'), 
     URL = '', 
     $el; 
     console.log(contentHeight); 

    $mainContent.height(contentHeight);  
    $internalLinks.each(function() { 

     $el = $(this); 
     $el.attr('href', '#' + this.pathname); 

    }).click(function() { 

     $ajaxSpinner.fadeIn(); 
     $mainContent.animate({opacity: '0.1'}); 
     $('.current_page_item').removeClass('current_page_item'); 
     $el = $(this); 
     URL = $el.attr('href').substring(1); 
     URL += ' #inside'; 
     $mainContent.load(URL, function() { 
     marquee(); 
     $ajaxSpinner.fadeOut(); 
     $mainContent.animate({opacity: '1'}); 
     $el.parent().addClass('current_page_item'); 
     contentHeight = parseInt($mainContent.css('paddingTop')) + parseInt($mainContent.css('paddingBottom')) + $('#inside').height(); 
     $mainContent.animate({height: contentHeight}); 
     }); 

    }); 

    if (hash != '' && hash != '#/') { 

     $ajaxSpinner.fadeIn(); 
     $mainContent.animate({opacity: '0.1'}); 
     $('.current_page_item').removeClass('current_page_item'); 
     URL = hash.substring(1); 
     URL += ' #inside'; 
     $('a[href="'+window.location.hash+'"]').addClass('current_link').parent().addClass('current_page_item'); 
     $mainContent.load(URL, function() { 
     marquee(); 
     $ajaxSpinner.fadeOut(); 
     $mainContent.animate({opacity: '1'}); 
     contentHeight = parseInt($mainContent.css('paddingTop')) + parseInt($mainContent.css('paddingBottom')) + $('#inside').height(); 
     $mainContent.animate({height: contentHeight});   
     }); 

    } 

}); 

这是老了,但它的作品,它的作用是,在主要内容区域中的任何链接上点击时,主内容将褪色成白色,AJAX微调器将出现,页面将加载AJAX上的链接到页面上。它也适用于刷新。

+0

我可能可以用这个欢呼声工作 – odd 2012-02-07 17:27:01