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!