仅使用一个查询字符串刷新Javascript
我有一个使用javascript自动刷新的页面,它使用URL中的所有参数进行刷新,例如server/ViewRoom.aspx?RoomID=5&ConfirmID=4834。我只想保留RoomID参数 我现在的代码是:仅使用一个查询字符串刷新Javascript,javascript,html,Javascript,Html,我有一个使用javascript自动刷新的页面,它使用URL中的所有参数进行刷新,例如server/ViewRoom.aspx?RoomID=5&ConfirmID=4834。我只想保留RoomID参数 我现在的代码是: <script type="text/javascript"> var timeout = setTimeout("location.reload(true);", 50000); // How often do you want the initi
<script type="text/javascript">
var timeout = setTimeout("location.reload(true);", 50000); // How often do you want the initial page to refresh?
function resetTimeout() {
clearTimeout(timeout);
timeout = setTimeout("location.reload(true);", 50000); // How often do you want the page to refresh?
}
</script>
var timeout=setTimeout(“location.reload(true);”,50000);//您希望初始页面多久刷新一次?
函数resetTimeout(){
clearTimeout(超时);
timeout=setTimeout(“location.reload(true);”,50000);//您希望页面多久刷新一次?
}
如何仅选择RoomID查询字符串进行重新加载我认为您不需要清除超时,因为重新加载发生了
<script type="text/javascript">
setTimeout( function () { location.href = location.href.split('&')[0]; }, 50000);
</script>
setTimeout(函数(){location.href=location.href.split('&')[0];},50000);
上面的代码通过传递您当前位置的modified href(first&之后的所有内容)href模拟refrash如Edmund24所述,您不需要清除超时,因为您正在重新加载页面,因此我们的代码如下所示:
<script type="text/javascript">
var timeout = setTimeout("location.reload(true);", 50000);
</script>
var timeout=setTimeout(“location.reload(true);”,50000);
类似地,我们不需要跟踪处理程序id,因此可以忽略存储:
<script type="text/javascript">
setTimeout("location.reload(true);", 50000);
</script>
setTimeout(“location.reload(true);”,50000);
Edmund代码的主要问题是,如果RoomID查询字符串不是URL中的第一个查询字符串,则其行为将不符合您的预期。因此,我们需要显式搜索并获取包含“RoomID”的查询字符串:
<script type="text/javascript">
setTimeout(function () {
var domain = location.href.split('?')[0],
queryStrings = location.href.split('?')[1].split('&'),
roomIDqs;
queryStrings.forEach(function (e, i, a) {
if(~e.indexOf('RoomID')) roomIDqs = e;
});
location.href = domain + roomIDqs;
}, 50000);
</script>
setTimeout(函数(){
var domain=location.href.split(“?”)[0],
queryStrings=location.href.split('?')[1]。split('&'),
室友;
forEach(函数(e,i,a){
如果(~e.indexOf('RoomID'))roomIDqs=e;
});
location.href=domain+roomIDqs;
}, 50000);
我还没有测试过这段代码,但这应该是我的想法
注意事项:
- ~.indexOf('other string')是contains()的快捷方式,因为javascript没有内置的contains()方法
- foreach(函数(e,i,a){})——回调函数自动传递当前元素(e)、当前索引(i)和完整数组(a)。我们在这里使用e来讨论当前的查询字符串
location.search=location.search.substring(0,location.search.indexOf('&'))
,如果RoomID
始终是第一个参数,我不这么认为,您可以通过.reload()
实现这一点,因为.reload()
只加载当前页面,无法控制url。相反,你可以做的是,提取当前页面url,从中删除不必要的参数,然后通过JS加载该页面。你可以将来自和location.href的答案组合在一起,而不是location.reload
。顺便说一句,你真的需要用JavaScript刷新页面吗?如果url是server/ViewRoom.aspx?ConfirmID=4834&RoomID=5如果在用户操作之后我们需要重置超时怎么办?:-)在这种情况下,是的,我们需要存储处理程序ID。但是这个问题没有提出任何与此相关的问题,所以我将其从我的回答中排除。