Can't attach local Javascript file to HTML

冷暖自知 提交于 2019-12-13 09:25:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!