我想实现一个JavaScript代码,该代码声明:如果页面已完全加载,请立即刷新页面,但只刷新一次。我被困在“只有一次”:
window.onload = function () {window.location.reload()}
这给出了一个没有“只有一次”的循环。如果这有帮助,加载jQuery。
我会说使用哈希,像这样:
window.onload = function() {
if(!window.location.hash) {
window.location = window.location + '#loaded';
window.location.reload();
}
}
var foo = true;
if (foo){
window.location.reload(true);
foo = false;
}
用这个
<body onload = "if (location.search.length < 1){window.location.reload()}">
使用window.localStorage ......像这样
var refresh = $window.localStorage.getItem('refresh');
console.log(refresh);
if (refresh===null){
window.location.reload();
$window.localStorage.setItem('refresh', "1");
}
这对我有用
最后,经过两个月的研究,我得到了一个重新加载页面的解决方案。
它在我的客户端JS项目上工作正常。
我写了一个函数,只在重载页面下面一次。
1)首先获得浏览器domloading时间
2)获取当前时间戳
3)浏览器domloading时间+ 10秒
4)如果浏览器domloading时间比当前时间戳大10秒,则可以通过“reloadPage();”刷新页面。
但如果它不超过10秒,则表示页面刚重新加载,因此不会重复重载。
5)因此,如果你调用“reloadPage();”在js文件页面的某个地方的函数只会重新加载一次。
希望能帮到别人
// Reload Page Function //
function reloadPage() {
var currentDocumentTimestamp = new Date(performance.timing.domLoading).getTime();
// Current Time //
var now = Date.now();
// Total Process Lenght as Minutes //
var tenSec = 10 * 1000;
// End Time of Process //
var plusTenSec = currentDocumentTimestamp + tenSec;
if (now > plusTenSec) {
location.reload();
}
}
// You can call it in somewhere //
reloadPage();
这是setTimeout的另一个解决方案,不完美,但它的工作原理:
它需要当前url中的参数,因此只需将当前网址的图像看起来像这样:
www.google.com?time=1
以下代码使页面只重新加载一次:
// Reload Page Function //
// get the time parameter //
let parameter = new URLSearchParams(window.location.search);
let time = parameter.get("time");
console.log(time)//1
let timeId;
if (time == 1) {
// reload the page after 0 ms //
timeId = setTimeout(() => {
window.location.reload();//
}, 0);
// change the time parameter to 0 //
let currentUrl = new URL(window.location.href);
let param = new URLSearchParams(currentUrl.search);
param.set("time", 0);
// replace the time parameter in url to 0; now it is 0 not 1 //
window.history.replaceState({}, "", `${currentUrl.pathname}?${param}`);
// cancel the setTimeout function after 0 ms //
let currentTime = Date.now();
if (Date.now() - currentTime > 0) {
clearTimeout(timeId);
}
}
接受的答案使用最少量的代码并且易于理解。我刚刚为此提供了另一种解决方案。
希望这有助于其他人。
使用rel =“external”就像下面的例子
<li><a href="index.html" rel="external" data-theme="c">Home</a></li>
当我遇到这个问题时,我搜索到这里,但大多数答案都试图修改现有的URL。这是另一个适用于我使用localStorage的答案。
<script type='text/javascript'>
(function()
{
if( window.localStorage )
{
if( !localStorage.getItem('firstLoad') )
{
localStorage['firstLoad'] = true;
window.location.reload();
}
else
localStorage.removeItem('firstLoad');
}
})();
</script>
<script type="text/javascript">
$(document).ready(function(){
//Check if the current URL contains '#'
if(document.URL.indexOf("#")==-1){
// Set the URL to whatever it was plus "#".
url = document.URL+"#";
location = "#";
//Reload the page
location.reload(true);
}
});
</script>
由于if条件,页面只会重新加载一次。我也遇到了这个问题,当我搜索时,我发现了这个很好的解决方案。这对我很好。
检查此链接它包含一个java脚本代码,您可以使用该代码仅刷新一次页面
刷新页面的方法有多种:
解决方法1:
要在每次打开时刷新页面,请使用:
<head>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
</head>
sollution2:
<script language=" JavaScript" >
<!--
function LoadOnce()
{
window.location.reload();
}
//-->
</script>
然后改变你说
<Body onLoad=" LoadOnce()" >
solution3:
response.setIntHeader("Refresh", 1);
但是,此解决方案将根据您指定的时间多次刷新页面
我希望这会对你有所帮助
我把它放在我想要重新加载的页面的标题内:
<?php if(!isset($_GET['mc'])) {
echo '<meta http-equiv="refresh" content= "0;URL=?mc=mobile" />';
} ?>
值“mc”可以设置为您想要的任何值,但两者必须匹配在2行中。并且“= mobile”可以是“= whatyouwant”它只需要一个值来停止刷新。
在</body>
标签后:
<script type="text/javascript">
if (location.href.indexOf('reload')==-1)
{
location.href=location.href+'?reload';
}
</script>
您需要使用GET或POST信息。 GET最简单。您的JS会检查URL,如果找不到某个参数,它不会只刷新页面,而是将用户发送到“不同”的URL,这将是相同的URL,但其中包含GET参数。
例如: http://example.com - >将刷新 http://example.com?refresh=no - >不会刷新
如果你不想要凌乱的URL,那么我会在主体的开头包含一些PHP,它会回显一个隐藏的值,表示在初始页面请求中是否包含了不刷新页面的必要POST参数。在此之后,您将包含一些JS以检查该值,并在必要时使用该POST信息刷新页面。
试试这个
var element = document.getElementById('position');
element.scrollIntoView(true);`
请尝试使用以下代码
var windowWidth = $(window).width();
$(window).resize(function() {
if(windowWidth != $(window).width()){
location.reload();
return;
}
});