0

I'm trying to add a JustGiving widget to my website but it doesn't display at all. I had other scripts in the file which I thought might be interfering so I ended up testing in a new plain html file and it still wouldn't work.

Interestingly enough if I copy/paste it into a W3 schools code editor it works! It's the same exact code and I cannot work out why it doesn't load in an html file.

    <!DOCTYPE html>
    <html>

    <head>
    </head>

    <body>
      <div id="jg-charity-events-board" data-config="id=45107">123</div>
      <script>
        (function(d) {
          var jg = d.createElement('script');
          jg.type = 'text/javascript';
          jg.async = true;
          jg.src = (d.location.protocol == 'https:' ? 'https://widgets.justgiving.com/charity-events-board/host.js' : 'http://widgets.justgiving.com/charity-events-board/host.js');
          var s = document.getElementsByTagName('script')[0];
          s.parentNode.insertBefore(jg, s);
        })(document);
      </script>
    </body>

    </html>
10
  • 1
    any errors in your browser's Console? Commented Jun 10, 2019 at 12:27
  • Is it case where your document doesn't have script tag on a page, there for selector can't resolve where to put it? "document.getElementsByTagName('script')[0]" == undefined ? Commented Jun 10, 2019 at 12:31
  • @Raimonds surely the <script> tag which contains the code above would be selected, no? Commented Jun 10, 2019 at 12:54
  • try disabling you Ad blocking extension? When I tried your code in jsFiddle after disabling Ad block, it worked. fiddle Commented Jun 10, 2019 at 12:59
  • 1
    @Raimonds "No because that script tag is not yet injected into dom" ...if the script is being executed, then it must have already been injected into the DOM and into the page. I just tested it and I'm right. Create a blank file and paste <!DOCTYPE html> <html> <body> <script> var s = document.getElementsByTagName('script')[0]; console.log(s); </script> </body> </html> into it. Open it in the browser and check the console. You'll see it logs the script tag. JS code can't be executed unless it's in the DOM Commented Jun 10, 2019 at 14:01

1 Answer 1

1

Why do you just not use something like this? Didn't test but I assume it works.

<div id="scripts"></div>
<script>
    var url = document.location.protocol + '//widgets.justgiving.com/charity-events-board/host.js';
    var script = '<script type="text/javascript" src="${url}"></script>';
    document.getElementById("scripts").innerHTML = script;
</script>

And jQuery has html and append functions for this.

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

2 Comments

I get a blank documents with: '; document.getElementById("scripts").innerHTML = script;
Did you try to print what happens? alert("test"), console.log("test")? Also try to inspect the page and view the page source. Try in different browsers.

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.