简体   繁体   English

是否可以使用任何文本编辑器插入(插入)代码或文本?

[英]Is it possible to inject (insert) codes or text with any text editors?

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. 比方说,我总共有100多个html现有文件。 I want to put this code before end of the head tag. 我想将此代码放在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>

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. 请帮助谢谢。

i suggest you do this at the server level, ie 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. 您可以从使用更灵活的框架(例如ruby-on-rails)中受益,该框架可以在不发生代码重复的情况下提供类似您的情况。

Use a perl or a python script to perform this kind of substitions. 使用perl或python脚本执行这种替换。

Its propably better to put the code in a .js resource file It ll be easier to maintain. 将代码放在.js资源文件中可能更好,而且维护起来更容易。

And the run a script like this : 然后运行这样的脚本:

perl -i.bak -pne "s,<body>,<body> <script src=\"/media/header.js\" type=\"text/javascript\"></script>," *.html

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

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