不能包含简单的javascript文件

时间:2019-04-04 11:10:53

标签: javascript php html

我正在尝试在我的php文件中包含一些非常简单的javascript文件。我有一个index.php,其中包含header.php和footer.php,以使我的代码更具模块化。

我将我的JavaScript包含在footer.php中,因此在index.php的末尾包含了footer.php。我的javascript文件的路径是:js / myscript.js

我的header.php:

<?php
include "inc/conn.php";
session_start();
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<?php
    if(isset($_SESSION["user"])){
        echo $_SESSION["user"];
    }
?>
<nav>
<?php
    if(isset($_SESSION["user"])){
        echo "<ol>";
        echo "<li><a href='#'>".$_SESSION["user"]."</a></li>";
        echo "<li><a href='inc/logout.php'>Logout</a></li>";
        echo "<li><a href='messages.php'>Messages</a></li>";
        echo "</ol>";
    }
?>
</nav>

我的index.php:

 <?php
include "header.php";
?>
    <label for="regist">Regist</label>
    <form id="regist" action="inc/regist.php" method="post" name="regist">
        <label for="registUsername">username:</label>
        <input type="text" id="registUsername" name="username" pattern="[a-z]">
        <label for="registPassword">password</label>
        <input type="password" id="registPassword" name="password">
        <input type="submit" name="submit" value="Regist">
    </form>

    <label for="login">Login</label>
    <form id="login" action="inc/login.php" method="post" name="login">
        <label for="loginUsername">username:</label>
        <input type="text" id="loginUsername" name="username">
        <label for="loginPassword">password</label>
        <input type="password" id="loginPassword" name="password">
        <input type="submit" value="Login" name="submit">
    </form>
<?php
include "footer.php";
?>

我的footer.php:

</body>
<script scr="js/myscript.js"></script>
</html>

出于仅尝试包含javascript文件的目的,我的javascript是:

alert("test");

如果我这样输入footer.php,它将起作用:

</body>
<script >
    alert("test");
</script>
</html>

如果我这样放置,它将无法正常工作,而且我找不到原因:

</body>
<script scr="js/myscript.js"></script>
</html>

1 个答案:

答案 0 :(得分:-4)

首先尝试将脚本放到身体末端之前