Javascript Onload只需工作一次
大家好,我的onload只工作了一次。因为当我刷新页面或者如果我要转到另一个页面时,它总是被加载。 以下是我目前的代码: JSJavascript Onload只需工作一次,javascript,jquery,html,Javascript,Jquery,Html,大家好,我的onload只工作了一次。因为当我刷新页面或者如果我要转到另一个页面时,它总是被加载。 以下是我目前的代码: JS <!--Auto load the on overlay function--> <script> window.onload = function openNav() { document.getElementById("myNav").style.width = "100%"; splashScreen(); }
<!--Auto load the on overlay function-->
<script>
window.onload = function openNav() {
document.getElementById("myNav").style.width = "100%";
splashScreen();
}
function closeNav() {
document.getElementById("myNav").style.width = "0%";
}
</script>
window.onload=函数openNav(){
document.getElementById(“myNav”).style.width=“100%”;
飞溅屏();
}
函数closeNav(){
document.getElementById(“myNav”).style.width=“0%”;
}
下面是我的splashscreen.js的js功能
//use onload when in your html the context is not inside
//the <header>
//create a splashScreen function
function splashScreen(){
//get the element on the html side with the id of "skip"
var skipButton = document.getElementById("skip");
//counter for the countdown
var counter = 5;
//create an element <p>
var newElement = document.createElement("p");
newElement.innerHTML = "Skip this 5 seconds";
var id;
skipButton.parentNode.replaceChild(newElement, skipButton);
id = setInterval(function(){
counter--;
if(counter < 0){
//replace the newElement on else condition statement
newElement.parentNode.replaceChild(skipButton, newElement);
clearInterval(id);
} else {
newElement.innerHTML = "You can skip this in " + counter.toString() + " seconds.";
}
}, 1000);
}
//在html中上下文不在内部时使用onload
//
//创建一个splashScreen函数
函数splashScreen(){
//获取html端id为“skip”的元素
var skipButton=document.getElementById(“跳过”);
//倒计时计数器
var计数器=5;
//创建一个元素
var newElement=document.createElement(“p”);
newElement.innerHTML=“跳过这5秒”;
变量id;
skipButton.parentNode.replaceChild(新元素,skipButton);
id=setInterval(函数(){
计数器--;
如果(计数器<0){
//替换else条件语句上的新元素
newElement.parentNode.replaceChild(skipButton,newElement);
清除间隔(id);
}否则{
newElement.innerHTML=“您可以在“+counter.toString()+”秒中跳过此操作。”;
}
}, 1000);
}
这就是我在我的html上对它的称呼
<html>
<body>
<!--SplashScreen JS-->
<script src="<?php echo base_url('assets/public/js/splashscreen.js');?>">
</script>
当您刷新页面或转到另一个页面,然后返回到原始页面时,页面正在重新加载,因此将调用onLoad
处理程序
如果您希望某些功能只发生一次,那么您可以尝试设置一个cookie,以便在页面加载时进行检查。如果设置了cookie,您就知道已经加载了一次,并且不再运行代码。如果没有,则运行代码,然后设置cookie
cookies的有用链接:
一种方法是设置窗口
的.name
属性,该属性在窗口
之后保持设置状态
const NAME = "once";
window.onload = function openNav() {
document.getElementById("myNav").style.width = "100%";
if (this.name !== NAME) {
this.name = NAME;
splashScreen();
} else {
// do other stuff
console.log(this.name)
}
}
为了能够知道已经为给定用户显示了启动屏幕,您需要存储一些在页面加载事件之间保持不变的内容。由于在单击刷新按钮时或在较早到达页面后返回页面时,页面将重新加载,因此此存储项不能位于页面本身中,因为每次页面加载时它都会重新初始化
Cookies(如另一个答案中所述)是一个选项,但我认为更简单。它的工作原理如下:
// Once the window is loaded...
window.addEventListener("load", function(){
// Check localStorage to see if the splash screen
// has NOT already been displayed
if(!localStorage.getItem("splash")){
// Splash has not been displayed, so show it:
splashScreen();
// Store a value in localStorage to denote that the splash screen
// has now been displayed
localStorage.setItem("splash", "true");
}
});
我是这样做的const NAME=“once”;window.onload=函数openNav(){document.getElementById(“myNav”).style.width=“100%”;如果(this.NAME!==NAME){splashScreen();}否则{alert(this.NAME);}}
在我的导航栏上我有gallery,我们的团队当我去我们的团队时仍然会弹出这个。名称
与窗口
的名称
属性不同。看到更新后的postyup更改了它,但仍然没有做任何事情,先生:(它仍然会弹出。或者使用localStorage@ScottMarcus主席先生,我怎样才能达到目标it@PaulKevinMacandili这是一个很好的教程:(或者你可以在下面看到Scott的答案,这可能与问题更相关)