Custom CSS feature to inject custom JavaScript
-
ok so, it turns out
$('body').on('action:ajaxifying')
no longer works, I think it got replaced byaction:ajaxify.end
and it's on thewindow
instead of thebody
.However, don't take my word for it yet, track that thread.Assuming, i am right, you should be able to do thatedit: @planner That should work
</style> <!-- gooogle ads --> <script async defer src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-8aa076493813069" data-ad-slot="8248656285" data-ad-format="auto"></ins> <script> // global _paq var _paq = _paq || []; _paq.push(["setCookieDomain", "*.linuxisi.com"]); _paq.push(["enableLinkTracking"]); _paq.push(["setTrackerUrl", ("https:" == document.location.protocol) ? "https" : "http") + "://linise.com/analyz/piwik.php"]); _paq.push(["setSiteId", "1"]); </script> <!-- then inject the piwic script, the old fashioned way, instead of writing the script tag --> <script async defer src="//linise.com/analyz/piwik.js"></script> <script> $(function() { $(window).on('action:ajaxify.end', function(e, data) { (adsbygoogle = window.adsbygoogle || []).push({}); // according to the piwik documentation, http://developer.piwik.org/api-reference/tracking-javascript, i should be to trigger page view like that _paq.push(["setDocumentTitle", document.domain + "/" + document.title]); // I set the document title every time, because it may have changed _paq.push(["trackPageView"]); }); }); </script> <style>
You do not have to have all of your javascript in the footer, it's true that it helps a teeny tiny bit with page performance, since scripts execution is usually synchronous, (but using the html5
async
anddefer
remedies that a bit for new browsers) anyways trust me, you will never feel the difference for small scripts like these. -
@DennisSun The widget is called "HTML", just add a javascript-tag
-
@frissdiegurke well , I tried to involve javascript code in the html widget. But it didn't work. I can't even see the whole homepage after that.
-
@DennisSun Any errors on console?
this example works fine for me:
<script type="text/javascript"> console.log('hello world!'); </script>
-
@frissdiegurke no error showed in log.
here is what I added:
<script type="text/javascript">
var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3F0da233db09b6a0886cd3f9d1a52e64d8' type='text/javascript'%3E%3C/script%3E"));
</script>The analytics code form baidu.com
-
yes,
document.write
replaces the whole site use sth like<script src='https://hm.baidu.com/h.js?0da233db09b6a0886cd3f9d1a52e64d8' type='text/javascript'></script>
this seems to be what the script is supposed to do (or
http://
if you don't use TLS). -
@frissdiegurke when using
document.write
you can avoid replacing the whole site, but it cannot be loaded asynchronously aka injected (as it needs to execute immediately on DOM build), which is most likely the case on most NodeBB pages@DennisSun if you want to have a dynamic the protocol, or maybe that string at the end, use something like
<script type="text/javascript"> (function() { var something = '0da233db09b6a0886cd3f9d1a52e64d8'; // or could be dynamically generated, I don't know // build your script tag var script = document.createElement('script'); script.type = 'text/javascript'; // that's useless, browsers ignore it, but whateves script.async = true; script.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'hm.baidu.com/h.js?' + something; // google analytics does that, this will find this actually script (unless loaded asynchronously, in that case it would find the last script tag on the page) // it should work in either case // var thisScriptTag = document.getElementsByTagName('script')[0]; // thisScriptTag.parentNode.insertBefore(script, s); // or you can just append it to the head var head = document.getElementsByTagName('head')[0]; head.appendChild(script); })(); </script>