如何使用javascript刷新页面内容并返回到同一页面
我有以下代码如何使用javascript刷新页面内容并返回到同一页面,javascript,jquery,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> 因此,如何刷新内容并返回到我正在使用的选项卡
<input type="reset" value="Reset" data-theme="b"
data-inline="true" onclick="refresh()" />
<script type="text/javascript">
function refresh() {
location.reload(true);
}
</script>
因此,如何刷新内容并返回到我正在使用的选项卡,而不是初始选项卡…当您刷新页面时,您会再次从浏览器缓存或服务器请求它。结果是页面上的任何状态都将被遗忘。您需要将状态保存在cookie或会话中,并在页面加载时将其还原。您可以通过在tab change事件中将当前状态存储在localstorage中,并在刷新时将其还原来完成此操作
var activeTab = localStorage.getItem('activeTab');
if(activeTab){
$('#myTab a[href="' + activeTab + '"]').tab('show');
}
请如果您要回拨服务器进行刷新,您需要存储正在显示的标记索引,例如,通过本地存储、cookie或作为参数传递给服务器(服务器将在刷新期间将您带回)。
如果您使用的是jquery ui,那么可以使用“活动”选项(请参见)我认为更好的方法是,不要按原样刷新页面,而是尝试将要加载的选项卡存储在url参数中。类似于:
www.example.com/your page.html?active tab={your\u active\u tab\u id}
显然,url参数的内容每次都必须更新,但可以将其存储在某种状态变量中,当调用函数时,从中获取值。最简单的方法是:- 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:编写代码从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');
}
});
函数refresh(){var-activeTab=localStorage.getItem('activeTab');location.reload(true);if(activeTab){$('#myTab a[href=“'+activeTab+'”)。tab('show');}我使用了下面的代码..它似乎不起作用..错误是什么..你不能用谷歌搜索并没有使这个答案错误。只是说说而已!
$(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');
}
});