是否可以使用任何文本编辑器注入(插入)代码或文本?
抱歉,我的标题一定很混乱。我想表达的意思是这样的。假设我总共有 100 多个 html 现有文件。我想将此代码放在 head 标签末尾之前。
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
我不想打开每个页面并手动插入。因为文件很多。我尝试使用 Find &替换,但它不允许我添加,因为它有很多行。
那么无论如何,很容易将代码注入到每个页面吗?
请好心人帮忙谢谢。
Sorry my title must be quite confused. What I want to mean is like this. Let's say, I have total more than 100 html existing files. I want to put this code before end of the head tag.
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
I don't want to open every page and insert manually. Because it's lots of file. I tried to do with Find & replace, but it doesn't allow me to add because it's have lots of lines.
So anyway to inject the codes to every page very easily ?
Please kindly help thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您在服务器级别执行此操作,即让服务器“注入”您想要的代码。
无论如何,代码多重性是您想要避免的。
每当您发现自己在复制和粘贴代码时,请停下来,深呼吸并再次思考。
您可以从使用更灵活的框架(例如 ruby-on-rails)中受益,它可以满足像您这样的情况,而不会导致代码重复。
i suggest you do this at the server level, i.e. have the server "inject" the code you want.
in any case, code multiplicity is something you want to avoid.
whenever you find yourself copying and pasting code, stop, take a deep breath and think again.
you could benefit from using a more flexible framework such as ruby-on-rails, which provides for situations like yours without incurring in code duplication.
使用 perl 或 python 脚本来执行此类替换。
将代码放在 .js 资源文件中可能会更好
维护起来会更容易。
并运行这样的脚本:
Use a perl or a python script to perform this kind of substitions.
Its propably better to put the code in a .js resource file
It ll be easier to maintain.
And the run a script like this :