2

This is frustrating, I'm trying to load a simple script inside my index.html but I get 404.

index.html

<head>
  <meta charset="utf-8">
  <title>Login</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root></app-root>
  <script type="text/javascript" src="./app/scripts/angular-cookies.js"></script>
</body>
</html>

This is my project structure

enter image description here

This is the error I get - 404

enter image description here

The file is there and it's not empty. I tried also:

<script type="text/javascript" src="../app/scripts/angular-cookies.js">

and

<script type="text/javascript" src="app/scripts/angular-cookies.js">

and

<script type="text/javascript" src="/app/scripts/angular-cookies.js">

Any help would be appreciated, thanks!

8
  • You're missing the first folder 'src' in your path to the scripts. Commented Jul 25, 2017 at 13:03
  • If you are using angular 2/4 this link maybe will help you to setup an external library inside your project thepolyglotdeveloper.com/2016/01/… Commented Jul 25, 2017 at 13:05
  • 2
    you need to put the file in assets and reference like: <script src="assets/yourfile.js">. I guess you're using Angular 2 so you should change angularjs tag Commented Jul 25, 2017 at 13:10
  • @Mickers it's not it, I've just tried Commented Jul 25, 2017 at 13:15
  • 1
    remember to do this to any other external resource like: scripts, styles, images, video etc. Commented Jul 25, 2017 at 13:20

2 Answers 2

5

As I've mentioned in comment, any external resource should be placed in assets folder and for your case referenced like <script type="text/javascript" src="assets/scripts/angular-cookies.js"> considering assets contains a folder named scripts which contains your angular-cookies.js file. After building the code the resulted www folder will be like:

  • assets -> which contains the external files
  • build -> which contains your ts files transpiled into js files
  • index.html
  • other files
Sign up to request clarification or add additional context in comments.

Comments

0

Try this and it will be fine:

<script type="text/javascript" src="/src/app/scripts/angular-cookies.js">

Or maybe

<script type="text/javascript" src="src/app/scripts/angular-cookies.js">

3 Comments

Sorry, it's not it.
There's no way you 'have' to put the scripts in an assets folder... You can put your scripts anywhere you like and as long as you reference the path correctly (which I see you're doing it wrong in a few places) it will work. Only thing I can think of is if you're using some loading library I've never heard of that requires an assets folder...
Sometimes that is depend on your project structure, have a try and let me know(But i think it will make no difference.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.