简体   繁体   English

带脚本标记的外部Javascript文件

[英]External Javascript File with Script tags

I am using a javascript templating framework called ICanHazjs.com. 我正在使用一个名为ICanHazjs.com的javascript模板框架。

My questions is can I include tags inside of a external javascript file, and if so is there any downsides / compatibility issues with this? 我的问题是我可以在外部javascript文件中包含标签,如果有的话,是否存在任何缺点/兼容性问题?

And if I can't what can I attach that will let me add tags. 如果我不能,我可以附加什么,这将让我添加标签。

If you mean can you include HTML markup inside the Javascript file, no that will not work. 如果你的意思是你可以在Javascript文件中包含HTML标记,那么这不会起作用。 The script will be parsed strictly as Javascript. 该脚本将严格解析为Javascript。

However, the included Javascript can create HTML markup and add it to the DOM. 但是,包含的Javascript可以创建 HTML标记并将其添加到DOM。

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 :) 对于document.write有一些常见的陷阱:)

ICanHaz supports adding templates at runtime with addTemplate . ICanHaz支持在运行时使用addTemplate添加模板。 It's probably your best bet. 这可能是你最好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM