简体   繁体   中英

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') + '.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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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