Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External Javascript File with Script tags

I am using a javascript templating framework called ICanHazjs.com.

My questions is can I include tags inside of a external javascript file, and if so is there any downsides / compatibility issues with this?

And if I can't what can I attach that will let me add tags.

like image 280
Adam Avatar asked Sep 14 '26 08:09

Adam


2 Answers

If you mean can you include HTML markup inside the Javascript file, no that will not work. The script will be parsed strictly as Javascript.

However, the included Javascript can create HTML markup and add it to the DOM.

like image 179
Michael Berkowski Avatar answered Sep 16 '26 22:09

Michael Berkowski


if you are only looking for scripts to append you can try this:

var script = document.createElement('script');
script.type = 'text/javascript';
script.id = 'foo_id';
script.src = 'myjs.js';
document.getElementsByTagName('head')[0].appendChild(script);

otherwise you might try

document.write('<h1>this is pretty ugly in here</h1>');

for document.write there are some common pitfalls :)

like image 30
Walialu Avatar answered Sep 16 '26 21:09

Walialu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!