Using External JavaScript with Script Tags

Publicado: 25 marzo, 2017 en Internet, Programación
Etiquetas:

To link to external JavaScript, use Script tags. These attributes will attach another JavaScript to the page, allowing further JavaScript code to then be run. A simple function can perform this processing by utilizing the DOM.

function addJavascript(jsname,pos) {
var th = document.getElementsByTagName(pos)[0];
var s = document.createElement(‘script’);
s.setAttribute(‘type’,’text/javascript’);
s.setAttribute(‘src’,jsname);
th.appendChild(s);
}

This function adds the necessary script tag into the end of the head or body section of the page. Call that function the name of the file that contains the extra code to be included and specify to add the function to the end of the head or the end of the body.

addJavascript(‘newExternal.js’,’body’);
addJavascript(‘newExternal2.js’,’head’);

Once the script tags have been added to the page, any functions can be called from within that file.

There is no need to write the same script over again to run the same JavaScript on several pages in a web site.  Instead, create an external JavaScript file with a .js extension, and refer to it using the src attribute in the <script> tag.

One thing that Javascript is often used for is to update the content of a web page. This is best done using the Document Object Model (DOM) but is sometimes done using innerHTML.

 Fuente: Link
Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s