JavaScript根据判断结果True跳转下页、False跳转首页的实现方法咨询
实现方案
你只需要在现有的switch分支判断中,对应"True"和"False"的case下添加页面跳转代码即可,JavaScript跳转页面常用两种方法:
- 使用
window.location.href = "目标页面地址":跳转后用户可通过浏览器后退按钮返回当前页 - 使用
window.location.replace("目标页面地址"):跳转后当前页不会保留在浏览历史中,无法后退返回
修改后的完整代码如下:
async function predict() { // predict can take in an image, video or canvas html element var image = document.getElementById("face-image") const prediction = await model.predict(image, false); prediction.sort((a,b) => parseFloat(b.probability) - parseFloat(a.probability)); let resultMessege; // 提前声明变量,避免全局污染 switch (prediction[0].className){ case "True": resultMessege = "성공" // 替换为你实际的下一页地址 window.location.href = "/next-page.html"; break; case "False": resultMessege = "실패." // 替换为你实际的首页地址 window.location.href = "/index.html"; break; default: resultMessege = "알 수 없는 오류가 발생하였습니다. 다시 시도해보시길 바랍니다." } $('.result-message').html(resultMessege); }
如果需要预留时间让用户看到提示文案再跳转,可以添加延时器,示例如下:
case "True": resultMessege = "성공" $('.result-message').html(resultMessege); // 1000为延时毫秒数,可根据需求调整 setTimeout(() => { window.location.href = "/next-page.html"; }, 1000) break;
内容的提问来源于stack exchange,提问作者Rhdehd
相关产品推荐
相关产品推荐

