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?
 
leakim971Connect With a Mentor PluritechnicianCommented:
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
 
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
 
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
All Courses

From novice to tech pro — start learning today.