injecting javascript into <body> tag

I have a large static site  with lots of static html pages. We want to put in a bit of Google Analytics(GA) tracking cod in all the pages. It is not possible to manually go through each page and insert the script.
However every page already calls a js file called topnav.js in the <head> section. I tried to inject the GA script when topnav.js is called like so

var jsref = document.createElement('script');
jsref.innerHTML="var gaJsHost = ((\"https:\" == document.location.protocol) ? \"https://ssl.\" : \"http://www.\");document.write(unescape(\"%3Cscript src='\" + gaJsHost + \"' type='text/javascript'%3E%3C/script%3E\"));";
      var jsref1 = document.createElement('script');
      jsref1.innerHTML="try {var pageTracker = _gat._getTracker(\"UA-xxxxxx-5\");pageTracker._setDomainName(\""+''+"\");pageTracker._setAllowLinker(true);pageTracker._setAllowHash(false);pageTracker._trackPageview();} catch(err) {}";

The code gets injected successfully but will not work. I think it needs to be in the body tag. I tried  this line


But the catch 22 situation is that when the topnav.js is being loded in the <head>, the <body> hasen't loaded yet so the above line fails.

Does anyone has any solution for this apart from adding the code manually.

Thanks to all.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ThinkPaperIT ConsultantCommented:
i dont think you can work with the existing javascript, if you need it to be in the body tag.

if all your pages are static, you can probably download a batch text editor like textpad or even use dreamweaver (if you already have it) to open all the pages at once, and do a batch search/replace.

However - this is assuming your <body> tag is the same or similar throughout. *you may need to do several runs of different variations of the <body> tag to ensure it replaces all of them properly*. It should be fairly painless.
Hans LangerCommented:
Try to include the js file and, when the body is loaded, call his methods, like this:

  <script type="text/javascript">
	var jsref1 = document.createElement('script');

	if (window.attachEvent){
		window.attachEvent("onload", doGACall);			
		window.addEventListener("DOMContentLoaded", doGACall, false);

	function doGACall(){

	  var pageTracker = _gat._getTracker("UA-xxxxxx-5");



Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.