gpt4 book ai didi

javascript - Onload只需要工作一次

转载 作者:行者123 更新时间:2023-12-03 03:47:11 28 4
gpt4 key购买 nike

大家好,我的 onload 只工作一次遇到了麻烦。因为当我刷新页面或者如果我要转到另一个页面时,它总是会加载。这是到目前为止我的代码:

JS

<!--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>

这是我的 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 上的称呼

  <html>
<body>
<!--SplashScreen JS-->
<script src="<?php echo base_url('assets/public/js/splashscreen.js');?>">
</script>

最佳答案

当您刷新页面或转到另一个页面,然后返回原始页面时,页面将重新加载,从而调用 onLoad 处理程序。

如果您希望某些功能只发生一次,那么您可以尝试的一件事是设置一个可以在页面加载时检查的 cookie。如果设置了 cookie,您就知道您已经加载了一次,并且不会再次运行代码。如果没有,则运行代码,然后设置 cookie。

cookie 的有用链接: https://www.w3schools.com/js/js_cookies.asp

关于javascript - Onload只需要工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45339607/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com