Customizing SMF > SMF Coding Discussion

Javascript posting code before </body> tag

(1/3) > >>

ukcwm:
Hi

Hope this is the correct forum place for this post...

I have been attempting to paste the following javascript into my index.template.php file just above the closing body tag as instructed. But it doesn't seem to be working. As you can see, I've added the '\' before each ' to stop parse errors - and there is no parse error, but the javascript doesn't seem to be operating as nothing appears on the page which should be if it were working properly. The same code works on other pages, but not the SMF pages. The code is already within an opened echo '


--- Code: ---<script src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>
<script src="/cookiescript/cookieControl-5.1.min.js" type="text/javascript"></script>
<script type="text/javascript"><!-- // --><![CDATA[
  cookieControl({
introText:\'<p>This site uses some unobtrusive cookies to store information on your computer. Unless you elect to disable cookies through your browser, by visiting and using this site you agree to our use of cookies as described in this policy.</p>\',
fullText:\'<p>Some cookies on this site are essential, and the site won\'t work as expected without them. These cookies are set when you submit a form, login or interact with the site by doing something that goes beyond clicking on simple links.</p><p>We also use some non-essential cookies to anonymously track visitors or enhance your experience of the site. If you\'re not happy with this, you can block, reset or delete cookies using your browser, but some nice features of the site may be unavailable.</p><p>To control third party cookies, you can also adjust your <a href="browser-settings" target="_blank">browser settings</a>.</p><p>By visiting and using our site you accept the terms of our (a href="/cookie-and-privacy-policy/">Cookie and Privacy Policy</a>.</p>\',
position:\'left\', // left or right
shape:\'triangle\', // triangle or diamond
theme:\'light\', // light or dark
startOpen:true,
autoHide:6000,
subdomains:true,
protectedCookies: [], //list the cookies you do not want deleted [\'analytics\', \'twitter\']
consentModel:\'information_only\',
onAccept:function(){ccAddAnalytics()},
onReady:function(){},
onCookiesAllowed:function(){ccAddAnalytics()},
onCookiesNotAllowed:function(){},
countries:\'\' // Or supply a list [\'United Kingdom\', \'Greece\']
});

function ccAddAnalytics() {
  jQuery.getScript("http://www.google-analytics.com/ga.js", function() {
var GATracker = _gat._createTracker(\'\');
GATracker._trackPageview();
  });
}
// ]]></script>

--- End code ---

Any ideas why it might not be working? Any help greatly appreciated.

Many thanks for your help

Kind regards
Trevor.

Suki:
Since the script is using jquery methods you need to load jquery first, put this before your script o load jquery:


--- Code: ---<script type="text/javascript">!window.jQuery && document.write(unescape(\'%3Cscript src="http://code.jquery.com/jquery.min.js"%3E%3C/script%3E\'))</script>

--- End code ---

SMF doesn't load jquery by itself, thats why the script doens't work with SMF files, with 2.1 this will change as jquery will be loaded by default.

ukcwm:
Hi

Thank you for your response. I tried adding the line of code you've suggested - but still no joy :(

Any other thoughts?

Thank you for your help :)

MrPhil:
Look at the "fullText:" line. You've got a quoted string with apostrophes within it.

--- Code: ---fullText:\'<p>Some cookies on this site are essential, and the site won\'t work as ... \',
--- End code ---
After PHP has played with it, what should be sent to the browser is

--- Code: ---fullText:'<p>Some cookies on this site are essential, and the site won't work as ... ',
--- End code ---
and the string ends early.

I think you want

--- Code: ---fullText:\'<p>Some cookies on this site are essential, and the site won\\\'t work as ... \',
--- End code ---
which ends up on the browser as

--- Code: ---fullText:'<p>Some cookies on this site are essential, and the site won\'t work as ... ',
--- End code ---

butch2k:
Run your site with Chrome console open and check the errors messages pertaining to Javascript and post them here.

You might want to double check your JS source path as well: src="/cookiescript/cookieControl-5.1.min.js", it might not point where you think it is. It's better to rely on the default theme path PHP variables, as done by SMF in the header JS.

Navigation

[0] Message Index

[#] Next page

Go to full version