defeated Posted January 7, 2016 Share Posted January 7, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/300216-javascript-within-javascript/ Share on other sites More sharing options...
kaleshwarchand Posted January 9, 2016 Share Posted January 9, 2016 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/300216-javascript-within-javascript/#findComment-1529358 Share on other sites More sharing options...
Muddy_Funster Posted January 12, 2016 Share Posted January 12, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/300216-javascript-within-javascript/#findComment-1529497 Share on other sites More sharing options...
defeated Posted January 13, 2016 Author Share Posted January 13, 2016 How about combining php and js? use php for the logic and put in js code if its the page you want it on. Unfortunately I can't in this instance. That is what I would have done, but the site won't run PHP. Quote Link to comment https://forums.phpfreaks.com/topic/300216-javascript-within-javascript/#findComment-1529572 Share on other sites More sharing options...
defeated Posted January 13, 2016 Author Share Posted January 13, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/300216-javascript-within-javascript/#findComment-1529573 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.