Jump to content

Recommended Posts

Hi, Javascript isn't my thing at all. However in this instance I have to use javascript instead of php.

 

I'm trying to add some Google experiment code to just some pages of a site. Here's what I have so far...

<script type=“text/javascript”>
	var theUrl = window.location.href;
	var lWr=theUrl.toLowerCase();
	if(lWr.indexOf(‘the/page/path’) >=0){

<!-- Google Analytics Content Experiment code -->
<script>function utmx_section(){}function utmx(){}(function(){var
k='63538864-5',d=document,l=d.location,c=d.cookie;
if(l.search.indexOf('utm_expid='+k)>0)return;
function f(n){if(c){var i=c.indexOf(n+'=');if(i>-1){var j=c.
indexOf(';',i);return escape(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/ga_exp.js?'+'utmxkey='+k+
'&utmx='+(x?x:'')+'&utmxx='+(xx?xx:'')+'&utmxtime='+new Date().
valueOf()+(h?'&utmxhash='+escape(h.substr(1)):'')+
'" type="text/javascript" charset="utf-8"><\/sc'+'ript>')})();
</script><script>utmx('url','A/B');</script>
<!-- End of Google Analytics Content Experiment code -->

	}
</script>

Obviously that doesn't work, but I can't figure out how to make it work. 

 

Help please.

 

Link to comment
https://forums.phpfreaks.com/topic/300216-javascript-within-javascript/
Share on other sites

How about combining php and js? use php for the logic and put in js code if its the page you want it on.

<?php
if($need_analytics_on_page==true)//use your logic here
{
?>
<script type=“text/javascript”>

<!-- Google Analytics Content Experiment code -->
<script>function utmx_section(){}function utmx(){}(function(){var
k='63538864-5',d=document,l=d.location,c=d.cookie;
if(l.search.indexOf('utm_expid='+k)>0)return;
function f(n){if(c){var i=c.indexOf(n+'=');if(i>-1){var j=c.
indexOf(';',i);return escape(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/ga_exp.js?'+'utmxkey='+k+
'&utmx='+(x?x:'')+'&utmxx='+(xx?xx:'')+'&utmxtime='+new Date().
valueOf()+(h?'&utmxhash='+escape(h.substr(1)):'')+
'" type="text/javascript" charset="utf-8"><\/sc'+'ript>')})();
</script><script>utmx('url','A/B');</script>
<!-- End of Google Analytics Content Experiment code -->

</script>
<?php
} //endif
?>

Don't think you can nest script tags within script tags, and you shouldn't need to anyway.  Also, comments in html are different to javascript comments and the fact you have dropped some html comments (lines with <!-- ... -->) inside the top level script tags is probably upsetting the parser.  To comment in javascript use two forward slashes (//) to comment to the right of them on a single line, and use /* ... */ to comment a block on a single or multiple lines.

 

So strip out the second <script> tag and change the comments from html comments to javascript comments and see how that goes.

Don't think you can nest script tags within script tags, and you shouldn't need to anyway.  Also, comments in html are different to javascript comments and the fact you have dropped some html comments (lines with <!-- ... -->) inside the top level script tags is probably upsetting the parser.  To comment in javascript use two forward slashes (//) to comment to the right of them on a single line, and use /* ... */ to comment a block on a single or multiple lines.

 

So strip out the second <script> tag and change the comments from html comments to javascript comments and see how that goes.

Hi Muddy,

 

That's what I was thinking, but there are two sets of script tags within the google code and I don't want to send the solution to the client without being sure it's going to work. In theory it would, but I'm just not hot enough on JS to know for sure.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.