2

I want to add my google analytics JS in the HEAD tag. so I can try to configure with angular.json

"scripts": [
              "src/assets/js/jquery-3.4.1.min.js",
              "src/assets/js/general.js",
              { "input": "src/assets/js/google-analytics.js", "lazy": false }
            ]

but, not add in the head tag, all js are added at the end of the body tag.

one other option is, will add directly in index.html. but I need to add base on the environment.

Please share if any way to add JS in HEAD tag directly using angular.json

2
  • I think this is a duplicate of stackoverflow.com/questions/46923628/… Commented Sep 30, 2019 at 11:55
  • Yes, but I am searching direct solutions with angular.json with environment condition if there any. otherwise can go with index.html + file replace option of angular.json or answers of these duplicate questions. thanks Commented Oct 1, 2019 at 5:31

2 Answers 2

-1

try to add your JavaScript file at the top of head tag inside the index.html file of our main app.

you will find it at project-name/src/index.html

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I know, can use index.html for that. but I am searching for a dynamic solution with environment condition
-1

To load js file externally from assets

create service file add file to the assets and write path in array.

import { Injectable } from "@angular/core";
import { Injectable } from "@angular/core";


    declare var document: any;
    @Injectable({
        providedIn:'root'
    })
    export class ScriptService {
      private scripts: any = {};

      constructor() {
        ScriptConstant.forEach((script: any) => {
          this.scripts[script.name] = {
            loaded: false,
            src: script.src
          };
        });
      }

      load(...scripts: string[]) {
        var promises: any[] = [];
        scripts.forEach(script => promises.push(this.loadScript(script)));
        return Promise.all(promises);
      }
      loadAll() {
        var promises: any[] = [];
        ScriptConstant.forEach(script => {
          // promises.push(delay(1000));
          promises.push(this.loadScript(script.name));
        });
        return Promise.all(promises);
      }

      loadScript(name: string) {
        return new Promise((resolve, reject) => {
          //resolve if already loaded
          if (this.scripts[name].loaded) {
            resolve({ script: name, loaded: true, status: "Already Loaded" });
          } else {
            //load script
            let script = document.createElement("script");
            script.type = "text/javascript";
            script.src = this.scripts[name].src;
            if (script.readyState) {
              //IE
              script.onreadystatechange = () => {
                if (
                  script.readyState === "loaded" ||
                  script.readyState === "complete"
                ) {
                  script.onreadystatechange = null;
                  this.scripts[name].loaded = true;
                  resolve({ script: name, loaded: true, status: "Loaded" });
                }
              };
            } else {
              //Others
              script.onload = () => {
                this.scripts[name].loaded = true;
                resolve({ script: name, loaded: true, status: "Loaded" });
              };
            }
            script.onerror = (error: any) =>
              resolve({ script: name, loaded: false, status: "Loaded" });
            document.getElementsByTagName("body")[0].appendChild(script);
          }
        });
      }
    }

    interface Scripts {
        name: string;
        src: string;
      }
      export const ScriptConstant: Scripts[] = [
        { name: "multislider", src: "assets/js/multislider.js" },

      ];

Inject this ScriptService wherever you need it and load js libs like this

this.script.load('multislider').then(data => {
    console.log('script loaded ', data);
}).catch(error => console.log(error));

1 Comment

The behavior of this service is not the same as building the scripts directly into index.html at start. This service will actually introduce a delay, and not inject the script into index.html until the service is ran. And this answer does not answer the question about loading scripts using angular.json.

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.