Google Sites中验证脚本触发页面崩溃问题求助
问题分析与修复方案
你的代码存在两个核心问题导致页面崩溃:
- 表单默认提交行为未彻底阻止:虽然
onClick返回了false,但Google Sites的表单处理机制可能会忽略这个返回值,触发表单GET提交,导致页面刷新甚至冲突。 - 跳转逻辑顺序错误:
window.location跳转后立刻执行alert,alert会阻塞页面线程,而Google Sites的安全策略可能会对这种异常的页面跳转+阻塞行为触发崩溃。
修复后的代码
<form name="form1" method="GET"> <input name="codebox1" type="text" /> <input name="button1" type="submit" value="Check Code" /> </form> <script> document.querySelector('form[name="form1"]').addEventListener('submit', function(e) { // 彻底阻止表单默认提交行为 e.preventDefault(); const inputValue = this.codebox1.value.trim(); if (inputValue === "Doovhhlqjhbh") { window.alert("Correct Code. Connecting..."); // 先弹提示再跳转,避免阻塞导致的异常 setTimeout(() => { window.location.href = 'https://sites.google.com/view/the-mbd-project/There-are-secrets-to-be-discovered'; }, 100); } else { window.alert("Wrong Code, try again!"); } }); </script>
关键修改点
- 用
addEventListener监听表单submit事件,通过e.preventDefault()彻底阻止默认提交行为,避免Google Sites的表单机制干扰。 - 调整逻辑顺序:先弹出提示,再通过
setTimeout延迟跳转,防止alert阻塞导致的页面异常。 - 增加
trim()处理输入值,避免用户输入前后空格导致验证失败。
另外需要注意:Google Sites对自定义HTML/脚本有严格的安全限制,新版Google Sites嵌入代码时需遵循平台的安全规范,避免使用受限API或不符合规则的代码写法。
内容的提问来源于stack exchange,提问作者Komplik08
相关产品推荐
相关产品推荐

