简体   繁体   English

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

[英]How do i refresh the contents of a page and return to the same page using javascript

I have the following code我有以下代码

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

Here onclick of reset button..refresh function is called which refreshes the contents... The issue is that..I am using several tabs in a page..now when I refresh the contents in a particular tab,upon refreshing its going to the initial tab declared (ii tab2)这里调用重置按钮的 onclick..refresh 函数刷新内容......问题是..我在一个页面中使用了几个选项卡..现在当我刷新特定选项卡中的内容时,刷新它的目的是声明的初始选项卡(ii tab2)

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

So how can i refresh the contents and return back to the tab in which i was working and not to the initial tab...那么我如何刷新内容并返回到我正在工作的选项卡而不是初始选项卡...

You can do this by storing current state in localstorage in tab change event and restore them on refresh您可以通过在标签更改事件中将当前状态存储在 localstorage 中并在刷新时恢复它们来做到这一点

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

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

Please refer参考

The easiest way is to:-最简单的方法是:-

1. Get the current tab number in the below code: 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: Write the code to activate a tab from the url 2:编写代码以从 url 激活选项卡

$(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');
      }
});

If you are calling back to the server to refresh, you need store the tag index you're showing, by localstorage, cookie, or by passing to the server as a param (and the server will bring you back during refresh), for example.如果您要回调服务器进行刷新,则需要通过 localstorage、cookie 或通过作为参数传递给服务器(服务器会在刷新期间将您带回来)来存储您正在显示的标签索引,例如. If you are using jquery-ui, you can use 'active' option (see here )如果您使用的是 jquery-ui,则可以使用 'active' 选项(请参阅此处

I think that the better way to do it is, instead of refreshing the page as is, try to store the tab that you want to load in url parameter.我认为更好的方法是,不要按原样刷新页面,而是尝试将要加载的选项卡存储在 url 参数中。 Something like: www.example.com/your-page.html?active-tab={your_active_tab_id}类似于: www.example.com/your-page.html?active-tab={your_active_tab_id}

Obviously, the content of the url param must be updated each time, but can store it in some kind of state variable and when the function is called, get the value from it.显然,url 参数的内容每次都必须更新,但可以将其存储在某种状态变量中,并在调用函数时从中获取值。

When you refresh the page you request it again from the browser cache or the server.当您刷新页面时,您会再次从浏览器缓存或服务器请求它。 The result being that any state on the page will be forgotten.结果是页面上的任何状态都将被遗忘。 You will beed to save the state in a cookie or session and restore it upon page load.您将需要在 cookie 或会话中保存状态并在页面加载时恢复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM