0

I've posted a very similar question to this in the past but I'm still having trouble and I have new updates.

This is my website (http://cryptoportugal.com/) and I'm trying to insert a Javascript widget into my header which is currently not working. I'm using the 'Metro Magazine' theme.

I've tried using the plugin 'Header and Footer' but it'd only add it to the footer, for the header it just wouldn't work.

So I've went as far as opening the functions.php file for my theme and I've inserted the following code on the end:

function mytheme() {
  wp_register_script ('crypto', get_stylesheet_directory_uri() .      '/js/crypto.js', true);
  wp_enqueue_script ('crypto');
}

add_action( 'wp_enqueue_scripts', 'mytheme' );

And this is my script:

<script type="text/javascript">
baseUrl = "https://widgets.cryptocompare.com/";
var scripts = document.getElementsByTagName("script");
var embedder = scripts[ scripts.length - 1 ];
(function (){
var appName = encodeURIComponent(window.location.hostname);
if(appName==""){appName="local";}
var s = document.createElement("script");
s.type = "text/javascript";
s.async = true;
var theUrl = baseUrl+'serve/v1/coin/header?   fsym=BTC&tsyms=USD,EUR,CNY,GBP';
s.src = theUrl + ( theUrl.indexOf("?") >= 0 ? "&" : "?") + "app=" +    appName;
embedder.parentNode.appendChild(s);
})();
</script>

Still nothing goes on, feels like there isn't even anything happening! :(

Help would be extremely appreciated,

thank you!

  • I can see the script correctly loaded in your web. So, "nothing goes on" and "which is currently not working" has nothing to do with your question about inserting a script. I've also checked the source code of your script and it is a incorrectly written js code (and it is not exactly the js code you posted in the question). I vote to close this question as off-topic because is too localized and related with js syntax. – cybmeta Jan 31 '17 at 18:38
  • How do you 'see the script is correctly loaded' ? Also, coindesk.com uses the same script with no problems at all so I don't think it's on their end, sorry. – Luís Carvalho Jan 31 '17 at 18:39
  • In the source code of your page, check it yourself. Note what I said in the previous comment: your script is not exactly what you posted in the question; the script that you are loading in your web is incorrectly written (for example, it includes <script> tags in a js file). Please, note that js questions it out of the scope of this site. If you have questions about js, you can try to post your questions in StackOverflow. – cybmeta Jan 31 '17 at 18:43
  • But that's so awkward. I'm simply taking the script from here - cryptocompare.com/dev/widget/wizard/… and this is a script I've seen a few websites using so I'm not following why it'd be incorrectly written :( – Luís Carvalho Jan 31 '17 at 18:44
0

I just opened your website and checked your crypto.js is successfully embeded in your head. But you have some errors in your code that's is why it's not working.

Here is the code which i have found in your file.

jQuery(document).ready(function() {
<script type="text/javascript">
baseUrl = "https://widgets.cryptocompare.com/";
var scripts = document.getElementsByTagName("script");
var embedder = scripts[ scripts.length - 1 ];
(function (){
var appName = encodeURIComponent(window.location.hostname);
if(appName==""){appName="local";}
var s = document.createElement("script");
s.type = "text/javascript";
s.async = true;
var theUrl = baseUrl+'serve/v1/coin/header?fsym=BTC&tsyms=USD,EUR,CNY,GBP';
s.src = theUrl + ( theUrl.indexOf("?") >= 0 ? "&" : "?") + "app=" + appName;
embedder.parentNode.appendChild(s);
})();
</script>}

And this one is error free code replace this with your existing file.

jQuery(document).ready(function() {

baseUrl = "https://widgets.cryptocompare.com/";
var scripts = document.getElementsByTagName("script");
var embedder = scripts[ scripts.length - 1 ];
(function (){
var appName = encodeURIComponent(window.location.hostname);
if(appName==""){appName="local";}
var s = document.createElement("script");
s.type = "text/javascript";
s.async = true;
var theUrl = baseUrl+'serve/v1/coin/header?fsym=BTC&tsyms=USD,EUR,CNY,GBP';
s.src = theUrl + ( theUrl.indexOf("?") >= 0 ? "&" : "?") + "app=" + appName;
embedder.parentNode.appendChild(s);
})();
})
  • I've done these modifications but still it isn't loading :( thank you anyhow! – Luís Carvalho Jan 31 '17 at 20:01
  • The line after embedder.parentNode.appendChild(s); should be })();. Can you try making that change? – czerspalace Jan 31 '17 at 20:31
  • @czerspalace The widget now appears on my website but on the footer instead of the header! :( – Luís Carvalho Jan 31 '17 at 23:10
  • You need to place the code where you want the widget to show up in the body, not in the footer, so you should place in header. php after your <body> tag – czerspalace Feb 1 '17 at 1:44
  • @czerspalace I've done that but it still appears on the footer .. – Luís Carvalho Feb 1 '17 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.