Dynamically add script to head of page

I need to dynamically add some Google Optimizer code into the head of my page.

I am using the following JS and it seems to add ti because I have an alert in the js file and it is being called, yet google optimzer can't seem to see it and I can't see the reference to the file in the page source?

var scriptStart = document.createElement( 'script' );
scriptStart.type = 'text/javascript';
scriptStart.src ="/include/JS/semAB-top.js";
$(document.head).append(scriptStart);
AWSHelpdeskAsked:
Who is Participating?
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.

ezklCommented:
The first thing that I would do is check the path to the source file of the JS. Depending on your hosting environment, the path may need to be adjusted so that the file can be accessed.
0
leakim971PluritechnicianCommented:
Hello AWSHelpdesk,

Can we see "semAB-top.js" ?
Try to call one of the function inside with a button ?

Perhaps elements needed by the function are not loaded when you try to run it

Regards.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript" src="jquery.js"></script>
</head>
<body>
<script language="javascript">
	var scriptStart = document.createElement( 'script' );
	scriptStart.type = 'text/javascript';
	scriptStart.src ="myscript.js";
	$(document.head).append(scriptStart);
</script>
<input type="button" value="run script" onclick="runscript();" />
</body>
</html>

-----------------
myscript.js
-----------------
function runscript() {
	alert("ok");
}

Open in new window

0

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
AWSHelpdeskAuthor Commented:
Ah ha, maybe!

This is what is in the script
function utmx_section(){}
function utmx(){}
(function(){
		  var k='xxxxxx',d=document,l=d.location,c=d.cookie;
		  function f(n){
			if(c){var i=c.indexOf(n+'=');
			if(i>-1){var j=c.indexOf(';',i);
			return c.substring(i+n.length+1,j<0?c.length:j)}}}var x=f('__utmx'),xx=f('__utmxx'),h=l.hash;
			d.write('<sc'+'ript src="'+'http'+(l.protocol=='https:'?'s://ssl':'://www')+'.google-analytics.com'+'/siteopt.js?v=1&utmxkey='+k+'&utmx='+(x?x:'')+'&utmxx='+(xx?xx:'')+'&utmxtime='
+new Date().valueOf()+(h?'&utmxhash='+escape(h.substr(1)):'')+'" type="text/javascript" charset="utf-8"></script>')
			});

utmx("url",'A/B');

Open in new window

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

AWSHelpdeskAuthor Commented:
BTW there are no js errors on the page
0
leakim971PluritechnicianCommented:
Line 9, we have :

document.write('<script src= bla bla bla type="text/javascript" charset="utf-8"></script>');

So we've a script tag inside a script tag. It can't work.
<script language="javascript"> <-- your
   <script language="javascript"></script> <---- the google one
</script>

Open in new window

0
AWSHelpdeskAuthor Commented:
This is the code that Google Optimizer gave me to use and if I put it direclty into the code in <script> tags it does work it is jsut when I try and call it from an external js file that Google is not seeing it
0
leakim971PluritechnicianCommented:
I know.
This code run at page load and create a code to run in a script tag. If you put this code inside an other script tag it can't be run.
0
AWSHelpdeskAuthor Commented:
Why would Google give you this code to add then?
0
leakim971PluritechnicianCommented:
The code run fine if you don't put it in anothor script tag :

Replace :

<script language="javascript"> <-- your
   <script language="javascript"></script> <---- the google one
</script>

By :


<script language="javascript"> <-- your
</script>
<script language="javascript"> </script> <---- the google one

Open in new window

0
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
HTML

From novice to tech pro — start learning today.