如何刷新页面内容并使用javascript返回到同一页面

时间:2016-05-03 06:22:01

标签: javascript jquery

我有以下代码

<input type="reset" value="Reset" data-theme="b"
       data-inline="true" onclick="refresh()" />
<script type="text/javascript">
    function refresh() {
        location.reload(true);
    }
</script>

这里点击重置按钮。调用刷新内容的刷新功能...... 问题是..我在页面中使用了几个选项卡..当我刷新特定选项卡中的内容时,刷新它到声明的初始选项卡(i.i tab2)

$(function() {
    $("#tabs-1").tabs();
});
$(function() {
    $('#tabs-1 .ui-tabs-nav a[href="#tabs-2"], #tabs-2').addClass('status1');
});

那么如何刷新内容并返回到我工作的标签而不是初始标签...

5 个答案:

答案 0 :(得分:1)

您可以通过在tab change change事件中将当前状态存储在localstorage中并在刷新时恢复它们来实现此目的

var activeTab = localStorage.getItem('activeTab');

if(activeTab){
    $('#myTab a[href="' + activeTab + '"]').tab('show');
}

refer

答案 1 :(得分:1)

最简单的方法是: -

<强> 1。在以下代码中获取当前标签号:

function refresh()
{       
  // Write your code here to get current tab
  // Assume tab is #tab2
  //Url is: http: abcwebsite.com/a.php#tab2
  // location.reload(true);
  var current_url = ""; //Get the current page here
  window.location = current_url + "#tab2";
}

2:编写代码以激活网址

中的标签
$(function() {
    if(window.location.hash) {
          var activeTab = window.location.hash;
          // Write the code to active the tab active here
          $('#myTab a[href="' + activeTab + '"]').tab('show');
      } else {
          // No hash found
          // Write the code to active the the default tabe
          $('#myTab a[href="#tab1"]').tab('show');
      }
});

答案 2 :(得分:0)

如果要回拨服务器进行刷新,则需要存储您正在显示的标记索引,localstorage,cookie或作为参数传递给服务器(并且服务器会在刷新期间将您带回) , 例如。 如果您使用的是jquery-ui,则可以使用“有效”选项(请参阅here

答案 3 :(得分:0)

我认为更好的方法是,不要按原样刷新页面,而是尝试在url参数中存储要加载的选项卡。类似于:M.dll

显然,每次都必须更新url param的内容,但是可以将它存储在某种状态变量中,当调用该函数时,从中获取值。

答案 4 :(得分:-1)

刷新页面时,您会再次从浏览器缓存或服务器请求它。结果是页面上的任何状态都将被遗忘。您将在cookie或会话中保存状态,并在页面加载时恢复它。