Javascript window.onload函数在页面加载时调用,也在每次关闭覆盖时调用
我正在调试一些JS代码,问题是调用Javascript window.onload函数在页面加载时调用,也在每次关闭覆盖时调用,javascript,Javascript,我正在调试一些JS代码,问题是调用window.onload函数的频率比我预期的要高。以下是我的JS代码: var trueCountApp = (function(){ window.onload = function(){openRulesOverlay()}; document.getElementById("open-rules-overlay-button").onclick = function(){openRulesOverlay()}; document
window.onload
函数的频率比我预期的要高。以下是我的JS代码:
var trueCountApp = (function(){
window.onload = function(){openRulesOverlay()};
document.getElementById("open-rules-overlay-button").onclick = function(){openRulesOverlay()};
document.getElementById("select-rules-button").onclick = function(){saveRuleSelections()};
function openRulesOverlay(){
let blackjackRules;
if(sessionStorage.blackjackRules){
blackjackRules = JSON.parse(sessionStorage.blackjackRules);
for(let i=0; i<blackjackRules.length;i++){
document.getElementsByClassName("rule")[i].value = blackjackRules[i];
}
}
document.getElementById("rules_overlay").style.display = "block";
}
function saveRuleSelections(){
let blackjackRules = [];
let rules = document.getElementsByClassName("rule");
for(let i=0; i<rules.length;i++){
blackjackRules.push(rules[i].value);
}
let jsonBlackjackRules = JSON.stringify(blackjackRules);
sessionStorage.setItem("blackjackRules", jsonBlackjackRules);
document.getElementById("rules_overlay").style.display = "none";
}
})();
var trueCountApp=(函数(){
window.onload=function(){openRulesOverlay()};
document.getElementById(“打开规则覆盖按钮”).onclick=function(){openRulesOverlay()};
document.getElementById(“选择规则按钮”).onclick=function(){saveRuleSelections()};
函数openRulesOverlay(){
让黑人来统治;
if(sessionStorage.blackjackRules){
blackjackRules=JSON.parse(sessionStorage.blackjackRules);
对于(让i=0;i简单修复我的特殊情况:默认按钮类型为“提交”,因此页面将加载,表单将打开,然后当我按下按钮关闭表单时,实际上是重新加载页面并再次打开表单。只需将按钮类型更改为“按钮”.使用调试器设置断点。然后查看调用堆栈以查看调用的位置。