1

I've checked some example that demonstrate how to add script with URL dynamically in Angular

But I want to add this kind of stuff to <head>

<script>
  !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
  n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
  n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
  t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
  document,'script','https://connect.facebook.net/en_US/fbevents.js');

  fbq('init', '<FB_PIXEL_ID>');
  fbq('track', "PageView");
</script>
  
  

or

  <noscript>something</noscript>
      

or both.

Any ideas, examples?

3
  • Can't you add it in the index.html? Commented Nov 3, 2020 at 8:31
  • Does this answer your question? Adding script script dinamicaly to <head> in Angular Commented Nov 3, 2020 at 8:36
  • @StPaulis no, I get it from a API Commented Nov 3, 2020 at 8:36

4 Answers 4

8
+50

You can make an function, which gets the (first) head tag from the document and add an created script tag (with the code you specify in innerHTML) before the first child of the head tag:

export function addScriptsToHead() {
  const head = document.getElementsByTagName('head')[0];

   
  const script = document.createElement('script');
  script.innerHTML = 'your code';

  head.insertBefore(script, head.firstChild);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can simply get the reference of an element using getElementByTagName() method by passing the tag name. After that you have to create a script variable then insert code to the variable using innerHTML attribute. Finally insert the script variable into head.

Try the following code,

  const head = document.getElementsByTagName('head')[0];
  const script = document.createElement('script');
  script.innerHTML = 'your code';
  head.insertBefore(script, head.firstChild);

Comments

1

If you can allow yourself to install a new library, use the HeadService that comes with @bespunky/angular-zen. It provides a safe and strongly-typed access to the head element.

Your component / service would look something like this:

 import { Component, OnInit } from '@angular/core';
 import { HeadService       } from '@bespunky/angular-zen/core';

 @Component({
     selector   : 'zen-head-service-demo',
     templateUrl: './head-service-demo.component.html',
     styleUrls  : ['./head-service-demo.component.css']
 })
 export class HeadServiceDemoComponent implements OnInit
 {
     constructor(private head: HeadService) { }

     ngOnInit()
     {
         this.head.addElement('script', /* config object or config function */);
     }
 }

Live examples | Service docs | Service API

🙋‍♂️ Remember to npm install @bespunky/angular-zen and import CoreModule from the /core package.


TLDR

💡 The HeadService has other cool features like looking up elements by attributes, support for well known elements (e.g. scripts and styles), etc.

💡 It's also testing-ready, meaning you can easily mock the document element or spy on it.

Comments

-6

here my example that i actually use in my project :

<head>
    <div *ngIf="dati">
        <title>{{dati.data.field_meta_tag.description}}</title>
    </div>
</head>

for do this you need to use rjxs operator and take the response to your api,in my example i use it only for the title now but you can use it for all.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.