0
I’ll introduce my test code summarized for better understanding.
index php.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Teste</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
script js.
function init() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = '//externo.com/js/encurtador.js';
document.body.appendChild(script);
}
window.addEventListener("DOMContentLoaded", init);
shortener.js (external, do not have access to the file)
document.addEventListener("DOMContentLoaded", function(e) {
// Codigo que faz a analise de todas tags <a> e insere o url encurtado
});
My Problem
I’m trying to load an external script in which it also makes use of 'Domcontentloaded''.
By my understanding (forgive me if I’m wrong) is that when the external script is inserted at the end of the <body>
the DOM is already completed, and due to this reason the external script cannot execute its functionality.
- You may be wondering why gave this wanting to do this, rather than simply add the script straight to the page?
It is that I intend to leave random this insertion , will be say (5 links) of several different companies of shortener, will be controlled by cookies when using one or other.
Sorry if I couldn’t explain my doubt clearly, I can try to be more detailed while doubting you if I didn’t understand something. I thank you from my heart who can help me, thank you very much :)
Since you are using PHP, it would not be easier to do this with it?
– Costamilam
Opa @Guilhermecostamilam , my test page is in . php running on an apache localhost server , but the place that will run is only page . html, I would also like to know if there is any correction to solve the problem using only javascript. Thank you very much for the comment :)
– Jeff.Dev