我在header.php文件中声明了JavaScript中的全局变量,但是在footer.php文件中无法访问它
码:
header.php文件
<?php define('BASE_URL','https://example.com'); ?>
<!DOCTYPE html>
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascipt">
var baseUrl = { url:"<?php echo BASE_URL; ?>" };
</script>
<meta charset="utf-8"/>
</head>
<body>
Run Code Online (Sandbox Code Playgroud)
的index.php
<?php
require_once("templates/header.php");
require_once("templates/footer.php");
Run Code Online (Sandbox Code Playgroud)
footer.php
<script type="text/javascript">
alert(baseUrl.url);
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在这里,我尝试访问footer.php文件中的baseUrl变量,但是我收到此错误
未捕获的ReferenceError:未定义baseUrl
寻求帮助!
你拼错了type="text/javascript".修复它,它应该工作.