Estoy tratando de cargar algo de html y javascript desde un sitio web sin usar iframe. Puedo renderizar el html, css y parece que los archivos javascript se están cargando, pero no puedo llamar a funciones desde el contenido cargado.
Estoy tratando de cargar usando fetch:
window.onload = function () { fetch(myURL) .then(function (response) { return response.text(); }) .then(function (body) { var dv = document.createElement('div'); dv.innerHTML = body; document.body.appendChild(dv); //trying to call function here after appending the html content to my page }); }();¿Es posible o está bloqueado por los navegadores por razones de seguridad?
tiene que agregar explícitamente un elemento de secuencia de comandos.
entonces lo que funciona con seguridad es:
let text = "<h1>Test</h1><script>const callMe = () => { alert('called'); }" + "</script>"; const scriptStart = text.indexOf('<script>'); const scriptEnd = text.indexOf('</script>'); const script = text.substring(scriptStart + "<script>".length, scriptEnd); text = text.substring(0, scriptStart) + text.substring(scriptEnd + "</script>".length); element.innerHTML = text; const scriptElement = document.createElement("script"); scriptElement.innerHTML = script; document.head.appendChild(scriptElement); callMe();