Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.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 在手动页面刷新时维护通过AJAX加载的状态/内容_Javascript_Jquery_Html_Ajax_Sinatra - Fatal编程技术网

Javascript 在手动页面刷新时维护通过AJAX加载的状态/内容

Javascript 在手动页面刷新时维护通过AJAX加载的状态/内容,javascript,jquery,html,ajax,sinatra,Javascript,Jquery,Html,Ajax,Sinatra,jQuery/History.js代码通过Ajax加载内容,方法是单击链接并在浏览器中前后导航,但在手动页面刷新时,历史堆栈/日志丢失,而不是仅加载我之前加载的相同内容,我得到了实际的.erb文件并丢失了页面的样式/格式 $(function(){ $('.nav-bar li a').on('click', function(event){ var urlPath = $(this).attr('href'); // not implemeneted var tit

jQuery/History.js代码通过Ajax加载内容,方法是单击链接并在浏览器中前后导航,但在手动页面刷新时,历史堆栈/日志丢失,而不是仅加载我之前加载的相同内容,我得到了实际的.erb文件并丢失了页面的样式/格式

$(function(){
  $('.nav-bar li a').on('click', function(event){
    var urlPath = $(this).attr('href');
    // not implemeneted
    var title = urlPath.capitalize();

    loadContent(urlPath);

    // pushes the current page state onto the history stack, and changes the URL
    History.pushState('', null, urlPath);
    event.preventDefault();
  });



  // This event makes sure that the back/forward buttons work too
  window.onpopstate = function(event){
    console.log("pathname: " + location.pathname);
    loadContent(location.pathname);
  };
});

function loadContent(url){
  // Uses jQuery to load the content
  $('#content').load(url+'#content', function(){
    console.log("Ajax success"); // Ajax

    // bxSlider image slider
    $('.bxslider').bxSlider({
      adaptiveHeight: true,
      mode: 'fade',
      captions: true
    });
  });
}

String.prototype.capitalize = function(){
  // add capitalize to string - uppercase the first character, add it to the rest of the  word 
  return this.charAt(0).toUpperCase() + this.slice(1);
}
如何在手动页面刷新过程中保持数据/历史记录?我尝试将当前html设置为变量,并将其作为pushState()中第一个参数的JSON对象传递,但我相信我可能做得不对。实现这一点的最佳方式是什么?另外,window.onpopstate函数不是在手动页面刷新时运行的,因此我不确定如何访问对象并获取数据

我不确定这有多重要。。。但我在Sinatra运行这个应用程序

我的布局.erb

   <!DOCTYPE html>
    <html>
    <head>
    <title>/title>

    <link href="http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css"  rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="/css/style.css" media="screen"/>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script  src="//browserstate.github.io/history.js/scripts/bundled/html4+html5/jquery.history.js">  </script>

   <script src="/js/jquery.bxslider.js"></script>
   <script src="/js/jquery.bxslider.min.js"></script>
   <link href="/css/jquery.bxslider.css" rel="stylesheet" type="text/css" />

   <script src="/js/application.js"></script>
   <link rel="shortcut icon" href="/images/favicon.ico" type="image/x-icon">
   </head>

  <body>
   <nav class="nav-bar">
    <ul>
      <li><a href="/" id="nav-bar">Home</a></li>
      <li><a href="about" id="nav-bar">About</a></li>
      <li><a href="portfolio" id="nav-bar">Portfolio</a></li>
      <li><a href="resume" id="nav-bar">Resume</a></li>
    </ul>
  </nav>
    <%= yield %>
  </body>

/标题>
my.erb视图的其余部分由一个div组成,其中包含内容ID和其他HTML标记。此外,当ajax调用起作用时,它似乎会在ajax成功时附加第二个div和内容ID。我如何防止这种情况

TL;博士

如何通过手动刷新来保持页面状态,而不是在实际视图中加载并丢失格式/样式。

您已经定义了

   // This event makes sure that the back/forward buttons work too
   window.onpopstate = function(event){
     console.log("pathname: " + location.pathname);
     loadContent(location.pathname);
   };
在jQuery块中:

 $(function(){
   ...
 )
这将强制它在加载文档后运行,因此直到第一个popstate事件发生后才定义它。

您已经定义了

   // This event makes sure that the back/forward buttons work too
   window.onpopstate = function(event){
     console.log("pathname: " + location.pathname);
     loadContent(location.pathname);
   };
在jQuery块中:

 $(function(){
   ...
 )

这将强制在加载文档后运行它,因此直到第一个popstate事件发生后才定义它。

您可以使用
localStorage
,在第一个页面中存储数据,在任何其他页面上再次获取数据

您可以使用
localStorage
,它在第一页存储数据,在任何其他页面再次获取数据