0

I need to add the following code to the footer of my page, but I have to do it using javascript code.

Code I need to add:

  <script type="text/javascript">
    (function (d, w, c) {
        (w[c] = w[c] || []).push(function() {
            try {
                    w.yaCounter39519115 = new Ya.Metrika({
                    id:39519115,
                    clickmap:true,
                    trackLinks:true,
                    accurateTrackBounce:true
                });
            } catch(e) { }
        });

        var n = d.getElementsByTagName("script")[0],
            s = d.createElement("script"),
            f = function () { n.parentNode.insertBefore(s, n); };
        s.type = "text/javascript";
        s.async = true;
        s.src = "https://mc.yandex.ru/metrika/watch.js";

        if (w.opera == "[object Opera]") {
            d.addEventListener("DOMContentLoaded", f, false);
        } else { f(); }
    })(document, window, "yandex_metrika_callbacks");

What I tried to add:

 $('#footer').append('<script type="text/javascript">' + (function (d, w,.... + '</script>');

However, this does not work for me.

7
  • What are you trying to do? Can you just put this code on the page? Commented Sep 9, 2016 at 14:13
  • Why do you want to put in footer ? it can be executed from anywhere Commented Sep 9, 2016 at 14:14
  • 1
    w[c] = w[c]???? Commented Sep 9, 2016 at 14:16
  • the function need to be inside string otherwise it will be exected before appening the script and the result of the function execution will be appended to script in your case it will be <script>undefined</script> Commented Sep 9, 2016 at 14:17
  • @degr that's how you add default argument in javascript before ES6, foo = foo || []; Commented Sep 9, 2016 at 14:17

1 Answer 1

3

Place your javascript in a separate .js file and then you need to use the document.createElement method to add/append your script to your footer block.

Try the below snippet.

var scriptSource = document.createElement('script');
var scriptURL = 'your_script_file_url_goes_here';
scriptSource.type = 'text/javascript';
scriptSource.src = scriptURL ;
$("#footer").append(scriptSource);

Hope this helps!.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.