开发者

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. 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') + 开发者_如何学JAVA'.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, 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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜