0

I'm working on a new website for my company, and we need to transfer hundreds of .html files to the new site. These .html do not currently contain any Google Analytics information, but I want to take this opportunity to add a reference to the .js file where the GA is stored.

My question is: how can I add the .js reference to these hundreds of files quickly and easily, without having it edit each one?

I'm guessing that there is either a application that can parse hundreds of HTML files, identify a specific part of that file (such as the <head> element) and automatically insert new text following it (e.g. <script type="text/javascript" src="script.js">).

I've not been able to find any such software, and by typing this question there are no "questions with similar titles" or "similar questions" found here.

Any ideas?

4
  • 1
    A perl script? Reliably parsing HTML is not so easy, but just looking for a </body> or something shouldn't be too hard. It depends on how messy the HTML is. Commented Sep 16, 2011 at 13:35
  • You can always write such software, I think this would be easy knowing at least one programming language (Pyhton, C++, etc.) This can be written even in php :) Commented Sep 16, 2011 at 13:36
  • Sorry I've not responded to all the comments and answers since I posted this question. I've been out of the office (unwell), but I'm back now and reviewing all the suggestions. Commented Sep 20, 2011 at 19:31
  • Writing such software, be it in C# (my language of choice) or something new like PHP could be done, sure, but it'll take time I don't have to write and/or learn. I'm really looking for an existing application that can do this, or a simple option that won't take too long to implement. Thanks though Commented Sep 20, 2011 at 19:33

5 Answers 5

3

There are lots of suggestions to do a search and replace, which will work. But the proper approach is to leverage whatever web platform you are using, and create a shared layout for the files that share a common design. Then, your common includes go there.

Otherwise, the next time Google changes their include code, or you decide to add a new .js library, or change anything else across the board, you will be doing this all over again. And it gets harder each time.

Also, you do not want to insert the include after the <head> tag, you want it before the closing </body> tag, so it does not stall the loading of the rest of the page.

Sign up to request clarification or add additional context in comments.

1 Comment

Good call about the </body> vs. <head> placement. As for my goal.. I want to include a reference to a .js file where all the GA code will go, so even in Google change the code, I only have one place to update it. My issue to how to get the reference to that .js file into the pages.
1

I think Notepad++ or Dreamweaver would be able to do what you're looking for. I believe you can do a find/replace by directories - and/or multiple files. You could have it find the tag and then put the reference right after the head tag.

2 Comments

Dreamweaver, eh? You know, I've been using that for the last 5 years and didn't know it had this type of functionality. I'll look into that too, along with Alex's suggestions above. Of course, if your suggestion does what I need, and so does Alex's, I may have to flip a coin to see who gets the magic green check mark!
The magic green check mark - my precious! ha! Dreamweaver does indeed have that functionality. I'm not sure which version they got it in, but it's there. I didn't realize it had it until about a year ago when I needed to do something similar and my buddy told me that. I didn't believe him until I used it... I had to buy him a pizza. A green check mark would've been cheaper...
1

Open all of your files you want to change in a text editor with some sort of multiple-file management ability (TextMate, Coda, even XCode, Notepad++ etc.) then do a global find and use the "replace in project" (or similarly name).

Find the closing body tag, and replace it with the javascript you want and the closing body tag.

Find: </body>
Replace: <script src="somescript.js" type="text/javascript"></script></body>

1 Comment

Alex, I think you just won the prize of the day (which is my thanks and has no redeemable value.. sorry). Textmate, Coda and XCode could well be the applications I have been looking for. I'll check it out, and come back to mark your suggestion as the answer if they prove to do exactly what I need. Watch this space!
1

You can include the following function in you global javascript file:

function insertGACode(){
    var gaCode = document.createElement('script');
    document.body.appendChild(gaCode);
    gaCode.innerHTML = "GA CODE HERE";
}

Run it whenever the page loads.

1 Comment

This is an interesting idea, and may work for some of the files I'm transferring to the new site, but there are hundreds of simple redirect file, that simply contain the bare minimum to do a META refresh that don't currently include a global javascript. I'll check out this idea of some files, and continue looking for the other files. Thanks :)
0

Even sed would do you here.

sed -i.bak 's/</head>/<script type="text/javascript" src="script.js"></head>/' *.html

2 Comments

and sed is? I'll Google it, but the answer alone doesn't give enough context, I don't think.
sed is the Unix stream editor. It can do things like this in a batch manner.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.