问题
I can't seem to attach my local Javascript file to the page:
<head>
<title>My Website</title>
<link rel="stylesheet" type="text/css" href="Main.css" />
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script>
</head>
<body ng-app="VideoGamesApp">
<div class="Main" ng-controller="myCtrl">
<div>
<p>{{test}}</p>
</div>
</div>
<script type="text/javascript" src='C:/Application/Application.js'></script>
</body>
The script:
var app = angular.module('VideoGamesApp', []);
app.controller('myCtrl', function ($scope) {
$scope.test = "Success";
});
When I write the script directly in the page it works, but not when i link it with "src".
回答1:
The src path is relative to the html file. If both are in the same directory, then try:
<script type="text/javascript" src='Application.js'></script>
if in a subdirectory
<script type="text/javascript" src='dirName/Application.js'></script>
if in a parent directory
<script type="text/javascript" src='../Application.js'></script>
However, make sure that the JS file is somewhere in the hierarchy of the root directory of your website, that is the root folder that your web server uses.
来源:https://stackoverflow.com/questions/31767363/cant-attach-local-javascript-file-to-html