Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cant add script file to components html

I have had a script file reference in index.html(root); index.html:

    <script src="page1.js"></script>
<script src="assets/sliderfunctions.js"></script>
<script src="assets/js/custom.js"></script>

no need sliderfunctions.js here, it contains some specific functions about slider so I carry it to slider.component.html file but as you guess it doesnt work as I expected actually it never loaded to browser..

slider.component.ts:

import {Component,ViewEncapsulation} from '@angular/core';

@Component({
    selector:'app-slider',
    templateUrl:'../templates/slider.component.html'
})
export class SliderComponent{
    constructor(){}
}

slider.component.html:

 <script src="page1.js"></script>
<script src="assets/sliderfunctions.js"></script>
<script src="assets/js/custom.js"></script>

(I carried 3 of them because order of scripts is important)so I want to get a specific point that I already make search on net but cant figure out how to add script file within component templates

like image 442
TyForHelpDude Avatar asked Dec 30 '16 18:12

TyForHelpDude


People also ask

How do I include a JavaScript file in HTML?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

Can I add script inside body?

You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

Where do you put scripts in HTML?

The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load. Generally, JavaScript code can go inside of the document <head> section in order to keep them contained and out of the main content of your HTML document.


1 Answers

The way you're thinking of is considered as security threat. You could either follow this answer

OR

You can refer script file from the component.ts file, just by doing System.import/require

System.import('filepath.js'); //or below one
require('filepath.js');
like image 82
Pankaj Parkar Avatar answered Sep 22 '22 23:09

Pankaj Parkar