sailr
asked on
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('sc ript');
jsref.innerHTML="var gaJsHost = ((\"https:\" == document.location.protocol ) ? \"https://ssl.\" : \"http://www.\");document.write(unesca pe(\"%3Csc ript src='\" + gaJsHost + \"google-analytics.com/ga. js' type='text/javascript'%3E% 3C/script% 3E\"));";
document.getElementsByTagN ame("head" )[0].appen dChild(jsr ef);
var jsref1 = document.createElement('sc ript');
jsref.setAttribute("type", "text/java script");
jsref1.setAttribute("type" ,"text/jav ascript");
jsref1.innerHTML="try {var pageTracker = _gat._getTracker(\"UA-xxxx xx-5\");pa geTracker. _setDomain Name(\""+' mydomain.c om'+"\");p ageTracker ._setAllow Linker(tru e);pageTra cker._setA llowHash(f alse);page Tracker._t rackPagevi ew();} catch(err) {}";
document.getElementsByTagN ame("head" )[0].appen dChild(jsr ef1);
The code gets injected successfully but will not work. I think it needs to be in the body tag. I tried this line
document.getElementsByTagN ame("body" )[0].appen dChild(jsr ef1);
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
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('sc
jsref.innerHTML="var gaJsHost = ((\"https:\" == document.location.protocol
document.getElementsByTagN
var jsref1 = document.createElement('sc
jsref.setAttribute("type",
jsref1.setAttribute("type"
jsref1.innerHTML="try {var pageTracker = _gat._getTracker(\"UA-xxxx
document.getElementsByTagN
The code gets injected successfully but will not work. I think it needs to be in the body tag. I tried this line
document.getElementsByTagN
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.