首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用浏览器的后退按钮获取ajax上一个wordpress帖子

使用浏览器的后退按钮获取ajax上一个wordpress帖子
EN

Stack Overflow用户
提问于 2013-04-15 12:39:55
回答 3查看 1.1K关注 0票数 0

我是新在ajax wordpress,我已经显示了与ajax的帖子,但现在我有问题,我想显示与ajax的前一篇文章,意味着我已经显示了与ajax的帖子,当用户点击一个帖子,它会显示,当点击浏览器后退按钮,然后以前的帖子没有show.Can任何人告诉我如何显示浏览器后退按钮以前的wordpress ajax帖子。下面是我使用的代码

代码语言:javascript
复制
 <script type="text/javascript">
    jQuery(document).ready(function($) {
    jQuery(".ajaxclick").click(function() {
    var post_id1 = $(this).parent("li").attr("id");
    var pageurl = $(this).attr('href');
    alert(pageurl);
    var ajaxURL = '<?php echo get_admin_url(); ?>admin-ajax.php';
    $.ajax({
    url: ajaxURL,
    type: 'POST',
    beforeSend: function() {
    $("#loading-animation").hide();
    $("#ajaxloader").show();
    },
    complete: function() {
    $("#loading-animation").show();
    $("#ajaxloader").hide();
    },
    data:   {
    action: 'load-content',
    post_id: post_id1 
    },
    success: function(response) {
    jQuery("#loading-animation").addClass('loadingstyle');
    jQuery("#loading-animation").html(response);
    return false;
    }

    });
    });
    });
    </script>
EN

回答 3

Stack Overflow用户

发布于 2013-04-15 12:49:40

使用散列#

代码语言:javascript
复制
$(function(){
   if(location.hash === 'somepage'){
     //do ajax again automatically here
   }
 });

并在成功时添加散列:

代码语言:javascript
复制
success: function(response) {
jQuery("#loading-animation").addClass('loadingstyle');
jQuery("#loading-animation").html(response, function(){
   location.hash = 'somepage';//<<-- must be identic with loaded page and same with above
});

祝好运!

票数 1
EN

Stack Overflow用户

发布于 2013-04-15 12:59:14

有一个jquery插件似乎可以有效地管理这一点

这个jQuery插件通过跨浏览器的HTML5 window.onhashchange事件实现了非常基本的可加书签的#散列历史。

http://benalman.com/projects/jquery-hashchange-plugin/ https://stackoverflow.com/a/173075/1061871

html5解决方案:https://developer.mozilla.org/en-US/docs/DOM/window.onpopstate

票数 0
EN

Stack Overflow用户

发布于 2013-04-23 12:54:56

我已经解决了这个问题,现在ajax帖子可以很好地使用浏览器的后退按钮和前进按钮,这段代码是在ajax帖子加载ajax后使用的,然后这段代码将用于显示浏览器的后退按钮,下面是代码:

代码语言:javascript
复制
    // Bind an event to window.onhashchange that, when the hash changes, 
$(window).on('hashchange', function(){
//check if hash tag exists in the URL
if(window.location.hash) {
var ajaxURL1 = '<?php echo get_admin_url(); ?>admin-ajax.php';
//set the value as a variable, and remove the #
var post_id1= window.location.hash.substring(1);
$.ajax({
url: ajaxURL1,
type: 'POST',
cache:false,
data:   {
action: 'load-content',
post_id: post_id1
},
success: function(response) {
jQuery("#loading-animation").addClass('loadingstyle');
jQuery("#loading-animation").html(response);
return false;
}   
});
}
else
{
window.location="http://203.134.217.4/testingwp/";
}
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16007756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档