繁体   English   中英

如何在.php文件中链接外部.js文件

[英]How to link external .js file in .php file

我试图简单地调用一个在外部文件中找到的JavaScript函数。 我的文件夹结构是:

C:\\ xampp \\ htdocs \\ test \\ index.php

C:\\ xampp \\ htdocs \\ test \\ js \\ functions.js

index.php

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="profile" href="http://gmpg.org/xfn/11">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" href="css/stylesheet.css"/>
    <title>Home</title>
  </head>

  <body>
    <?php include("header.php"); ?>
    <?php include("content.php"); ?>

    <script src="https://kit.fontawesome.com/f0aaae8537.js"></script>
    <script src="/js/functions.js"></script>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </body>

  <script>
    test();
   </script>
</html>

functions.js

function test(){
   alert("where am i");
}

我正在使用VS Code和XAMPP。 未调用功能测试。 当我在index.php中定义函数但不在外部文件中定义函数时,该函数起作用。 我试过包括属性type =“ text / javascript”,并将functions.js文件移到根目录“ test”,但还是没有。

js很可能不在根文件夹中,因此您的script标签可能需要为:

<script src="js/functions.js"></script>

注意js之前没有/

尝试这个

<script src="js/functions.js"></script>

在functions.js中

$(document).ready(function() {
   function test(){
     alert("where am i");
   }
});

删除/否则它将被视为从/ (运行文件的根目录,很可能是Web服务器)开始的路径:

<script src="js/functions.js"></script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM