injecting javascript into <body> tag

Hi
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 + \"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E\"));";
      document.getElementsByTagName("head")[0].appendChild(jsref);       
      
      var jsref1 = document.createElement('script');
      jsref.setAttribute("type","text/javascript");
      jsref1.setAttribute("type","text/javascript");
      jsref1.innerHTML="try {var pageTracker = _gat._getTracker(\"UA-xxxxxx-5\");pageTracker._setDomainName(\""+'mydomain.com'+"\");pageTracker._setAllowLinker(true);pageTracker._setAllowHash(false);pageTracker._trackPageview();} catch(err) {}";
      document.getElementsByTagName("head")[0].appendChild(jsref1);       

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

document.getElementsByTagName("body")[0].appendChild(jsref1);

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.
Sailesh
sailrAsked:
Who is Participating?
 
Hans LangerCommented:
Try to include the js file and, when the body is loaded, call his methods, like this:


<html>
 <head>
  <script type="text/javascript">
		
	var jsref1 = document.createElement('script');
	jsref1.setAttribute("src","http://www.google-analytics.com/ga.js");
	jsref1.setAttribute("type","text/javascript");
	document.getElementsByTagName("head")[0].appendChild(jsref1);


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


	function doGACall(){

	  var pageTracker = _gat._getTracker("UA-xxxxxx-5");
	  alert(pageTracker)
	  pageTracker._setDomainName("www.google.com");
	  pageTracker._setAllowLinker(true);
	  pageTracker._setAllowHash(false);
	  pageTracker._trackPageview();

	}
  </script>
 </head>

 <body>
  
 </body>
</html>

Open in new window

0
 
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.
http://www.textpad.com

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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.