首页 > 解决方案 > 为什么 javascript 函数在 html 参考中不起作用?

问题描述

function ClearAll() {
    localStorage.clear();
    doShowAll();
}
<?php
    session_start();
    header('Location: index.html');
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title>shopping cart</title>
        <script src="Storage.js"></script>
        <link rel="stylesheet" href="StorageStyle.css">
    </head>
    <body>
        <script>ClearAll();</script>
    </body>
</html>
<?php
    $_SESSION['loggued_on_user'] = NULL;
?>

我想注销并同时ClearAll从 js 文件执行功能。但是我在没有清理 js 中的内容的情况下获得了注销。

标签: javascripthtml

解决方案


header('Location: index.html');

当您发出 HTTP 重定向时,大多数客户端(包括所有主流浏览器)都会透明地跟随它。

HTTP 响应的正文(这是您的 HTML 文档,其中包含 JavaScript)仅由自动遵循重定向的客户端使用。

由于不呈现 HTML 文档,因此不执行 JavaScript。


您可能应该使用事件处理程序运行 JS,该click事件处理程序侦听用户在上一页单击以启动注销的按钮。


推荐阅读