简体   繁体   中英

How to append and run script on button click [JAVASCRIPT]

I need some help.

Every time a user clicks a button I need to append and run a JS script. The problem I need to evaluate some conditions to load and run the script...

What I have:

var loadScriptBtn = document.getElementById("loadScript");
loadScriptBtn.onclick = loadScript()

function getCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for (var i = 0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) == ' ') c = c.substring(1, c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
  }
  return null;
}

function loadScript() {
  if (getCookie('cookieAccepted')) {
    var serverScript = document.createElement("script");
    serverScript.type = "text/javascript";
    serverScript.src = "http://example.com/myscript";
    document.head.appendChild(serverScript);
  } else {
    alert('Please enable cookies')
  }
}

The script is being appended correctly in the body but it doesn't execute, and I need it to execute...

I looked in other questions but I couldn't find a solution that uses JavaScript, I can't use Jquery on this,

Thank you all for your time!

Try creating a new div, setting its innerHTML, then adding this new div to the DOM. Something like:

function loadScript() {
  if (getCookie('cookieAccepted')) {
    var serverScript = document.createElement("script");
    serverScript.type = "text/javascript";
    serverScript.src = "http://example.com/myscript";

    var newdiv = document.createElement('div');
    newdiv.innerHTML = serverScript;
    document.getElementById('target').appendChild(newdiv); //<-- lets say that you have a div called target in your html (<div id="target"></div>)
  } else {
    alert('Please enable cookies')
  }
}

At the moment you're assigning a string to the onclick . You want to assign a reference to the function instead:

loadScriptBtn.onclick = loadScript;

You can use Jquery.getScript to load script file correctly

$("button").click(function(){
  $.getScript("http://example.com/myscript");
});

https://www.w3schools.com/jquery/ajax_getscript.asp

or you can do that with pure javascript like below:

 //I added momentjs after page load. function loadFile() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { var s = document.createElement("script") s.innerText = this.responseText;//create script and append innerText document.head.appendChild(s);//add script element to head var test = moment('2016-01-01'); //test inserted js file. console.log(test); } }; xhttp.open("GET", "https://momentjs.com/downloads/moment.min.js", true); xhttp.send();//get url content }
 <div id="demo"> <button type="button" onclick="loadFile()">Load Script</button> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM