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

发布于 2024-10-30 20:10:17 字数 703 浏览 4 评论 0原文

抱歉,我的标题一定很混乱。我想表达的意思是这样的。假设我总共有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小伙你站住 2024-11-06 20:10:17

我建议您在服务器级别执行此操作,即让服务器“注入”您想要的代码。
无论如何,代码多重性是您想要避免的。
每当您发现自己在复制和粘贴代码时,请停下来,深呼吸并再次思考。
您可以从使用更灵活的框架(例如 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.

晨敛清荷 2024-11-06 20:10:17

使用 perl 或 python 脚本来执行此类替换。

将代码放在 .js 资源文件中可能会更好
维护起来会更容易。

并运行这样的脚本:

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

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 :

perl -i.bak -pne "s,<body>,<body> <script src=\"/media/header.js\" type=\"text/javascript\"></script>," *.html
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文