[英]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.